
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Terraform Test · 《Terraform入门教程》</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="大可不加冰">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism-vsc-dark-plus.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-image-captions/image-captions.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="7.Aha——会心一击.html" />
    
    
    <link rel="prev" href="5.22.5.show.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                        <b>1.1.</b>
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.Terraform初步体验.html">
            
                <a href="1.Terraform初步体验.html">
            
                    
                        <b>1.2.</b>
                    
                    Terraform 初步体验
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                        <b>1.3.</b>
                    
                    Terraform基础概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="2.1.Terraform基础概念——Provider.html">
            
                <a href="2.1.Terraform基础概念——Provider.html">
            
                    
                        <b>1.3.1.</b>
                    
                    Provider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="2.2.Terraform基础概念——状态管理.html">
            
                <a href="2.2.Terraform基础概念——状态管理.html">
            
                    
                        <b>1.3.2.</b>
                    
                    状态管理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="3.Terraform代码的书写.html">
            
                <a href="3.Terraform代码的书写.html">
            
                    
                        <b>1.4.</b>
                    
                    Terraform 代码的书写
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.1.类型.html">
            
                <a href="3.1.类型.html">
            
                    
                        <b>1.4.1.</b>
                    
                    类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="3.2.配置语法.html">
            
                <a href="3.2.配置语法.html">
            
                    
                        <b>1.4.2.</b>
                    
                    配置语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="3.3.输入变量.html">
            
                <a href="3.3.输入变量.html">
            
                    
                        <b>1.4.3.</b>
                    
                    输入变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="3.4.输出值.html">
            
                <a href="3.4.输出值.html">
            
                    
                        <b>1.4.4.</b>
                    
                    输出值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="3.5.局部值.html">
            
                <a href="3.5.局部值.html">
            
                    
                        <b>1.4.5.</b>
                    
                    局部值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="3.6.资源.html">
            
                <a href="3.6.资源.html">
            
                    
                        <b>1.4.6.</b>
                    
                    资源
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="3.7.数据源.html">
            
                <a href="3.7.数据源.html">
            
                    
                        <b>1.4.7.</b>
                    
                    数据源
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="3.8.表达式.html">
            
                <a href="3.8.表达式.html">
            
                    
                        <b>1.4.8.</b>
                    
                    表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="3.9.重载文件.html">
            
                <a href="3.9.重载文件.html">
            
                    
                        <b>1.4.9.</b>
                    
                    重载文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="3.10.代码风格规范.html">
            
                <a href="3.10.代码风格规范.html">
            
                    
                        <b>1.4.10.</b>
                    
                    代码风格规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.11" data-path="3.11.checks.html">
            
                <a href="3.11.checks.html">
            
                    
                        <b>1.4.11.</b>
                    
                    Checks
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="4.Terraform模块.html">
            
                <a href="4.Terraform模块.html">
            
                    
                        <b>1.5.</b>
                    
                    Terraform 模块
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="4.1.创建模块.html">
            
                <a href="4.1.创建模块.html">
            
                    
                        <b>1.5.1.</b>
                    
                    创建模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="4.2.使用模块.html">
            
                <a href="4.2.使用模块.html">
            
                    
                        <b>1.5.2.</b>
                    
                    使用模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="4.3.模块元参数.html">
            
                <a href="4.3.模块元参数.html">
            
                    
                        <b>1.5.3.</b>
                    
                    模块元参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="4.4.重构.html">
            
                <a href="4.4.重构.html">
            
                    
                        <b>1.5.4.</b>
                    
                    重构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="4.5.设计新模块的模式.html">
            
                <a href="4.5.设计新模块的模式.html">
            
                    
                        <b>1.5.5.</b>
                    
                    设计新模块的模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="5.Terraform命令行.html">
            
                <a href="5.Terraform命令行.html">
            
                    
                        <b>1.6.</b>
                    
                    Terraform 命令行
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="5.1.命令行配置文件.html">
            
                <a href="5.1.命令行配置文件.html">
            
                    
                        <b>1.6.1.</b>
                    
                    命令行配置文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="5.2.环境变量.html">
            
                <a href="5.2.环境变量.html">
            
                    
                        <b>1.6.2.</b>
                    
                    环境变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="5.3.资源地址.html">
            
                <a href="5.3.资源地址.html">
            
                    
                        <b>1.6.3.</b>
                    
                    资源地址
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="5.4.apply.html">
            
                <a href="5.4.apply.html">
            
                    
                        <b>1.6.4.</b>
                    
                    apply
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="5.5.console.html">
            
                <a href="5.5.console.html">
            
                    
                        <b>1.6.5.</b>
                    
                    console
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="5.6.destroy.html">
            
                <a href="5.6.destroy.html">
            
                    
                        <b>1.6.6.</b>
                    
                    destroy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="5.7.fmt.html">
            
                <a href="5.7.fmt.html">
            
                    
                        <b>1.6.7.</b>
                    
                    fmt
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="5.8.force-unlock.html">
            
                <a href="5.8.force-unlock.html">
            
                    
                        <b>1.6.8.</b>
                    
                    force-unlock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="5.9.get.html">
            
                <a href="5.9.get.html">
            
                    
                        <b>1.6.9.</b>
                    
                    get
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="5.10.graph.html">
            
                <a href="5.10.graph.html">
            
                    
                        <b>1.6.10.</b>
                    
                    graph
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="5.11.import.html">
            
                <a href="5.11.import.html">
            
                    
                        <b>1.6.11.</b>
                    
                    import
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.12" data-path="5.12.init.html">
            
                <a href="5.12.init.html">
            
                    
                        <b>1.6.12.</b>
                    
                    init
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.13" data-path="5.13.output.html">
            
                <a href="5.13.output.html">
            
                    
                        <b>1.6.13.</b>
                    
                    output
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.14" data-path="5.14.plan.html">
            
                <a href="5.14.plan.html">
            
                    
                        <b>1.6.14.</b>
                    
                    plan
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.15" data-path="5.15.providers.html">
            
                <a href="5.15.providers.html">
            
                    
                        <b>1.6.15.</b>
                    
                    providers
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.15.1" data-path="5.15.1.mirror.html">
            
                <a href="5.15.1.mirror.html">
            
                    
                        <b>1.6.15.1.</b>
                    
                    mirror
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.15.2" data-path="5.15.2.schema.html">
            
                <a href="5.15.2.schema.html">
            
                    
                        <b>1.6.15.2.</b>
                    
                    schema
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6.16" data-path="5.16.refresh.html">
            
                <a href="5.16.refresh.html">
            
                    
                        <b>1.6.16.</b>
                    
                    refresh
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.17" data-path="5.17.show.html">
            
                <a href="5.17.show.html">
            
                    
                        <b>1.6.17.</b>
                    
                    show
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.18" data-path="5.18.state.html">
            
                <a href="5.18.state.html">
            
                    
                        <b>1.6.18.</b>
                    
                    state
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.18.1" data-path="5.18.1.list.html">
            
                <a href="5.18.1.list.html">
            
                    
                        <b>1.6.18.1.</b>
                    
                    list
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.18.2" data-path="5.18.2.mv.html">
            
                <a href="5.18.2.mv.html">
            
                    
                        <b>1.6.18.2.</b>
                    
                    mv
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.18.3" data-path="5.18.3.pull.html">
            
                <a href="5.18.3.pull.html">
            
                    
                        <b>1.6.18.3.</b>
                    
                    pull
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.18.4" data-path="5.18.4.push.html">
            
                <a href="5.18.4.push.html">
            
                    
                        <b>1.6.18.4.</b>
                    
                    push
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.18.5" data-path="5.18.5.replace-provider.html">
            
                <a href="5.18.5.replace-provider.html">
            
                    
                        <b>1.6.18.5.</b>
                    
                    replace-provider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.18.6" data-path="5.18.6.rm.html">
            
                <a href="5.18.6.rm.html">
            
                    
                        <b>1.6.18.6.</b>
                    
                    rm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.18.7" data-path="5.18.7.show.html">
            
                <a href="5.18.7.show.html">
            
                    
                        <b>1.6.18.7.</b>
                    
                    show
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6.19" data-path="5.19.taint.html">
            
                <a href="5.19.taint.html">
            
                    
                        <b>1.6.19.</b>
                    
                    taint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.20" data-path="5.20.validate.html">
            
                <a href="5.20.validate.html">
            
                    
                        <b>1.6.20.</b>
                    
                    validate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.21" data-path="5.21.untaint.html">
            
                <a href="5.21.untaint.html">
            
                    
                        <b>1.6.21.</b>
                    
                    untaint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.22" data-path="5.22.workspace.html">
            
                <a href="5.22.workspace.html">
            
                    
                        <b>1.6.22.</b>
                    
                    workspace
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.22.1" data-path="5.22.1.list.html">
            
                <a href="5.22.1.list.html">
            
                    
                        <b>1.6.22.1.</b>
                    
                    list
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.22.2" data-path="5.22.2.select.html">
            
                <a href="5.22.2.select.html">
            
                    
                        <b>1.6.22.2.</b>
                    
                    select
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.22.3" data-path="5.22.3.new.html">
            
                <a href="5.22.3.new.html">
            
                    
                        <b>1.6.22.3.</b>
                    
                    new
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.22.4" data-path="5.22.4.delete.html">
            
                <a href="5.22.4.delete.html">
            
                    
                        <b>1.6.22.4.</b>
                    
                    delete
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.22.5" data-path="5.22.5.show.html">
            
                <a href="5.22.5.show.html">
            
                    
                        <b>1.6.22.5.</b>
                    
                    show
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter active" data-level="1.7" data-path="6.test.html">
            
                <a href="6.test.html">
            
                    
                        <b>1.7.</b>
                    
                    Terraform Test
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="7.Aha——会心一击.html">
            
                <a href="7.Aha——会心一击.html">
            
                    
                        <b>1.8.</b>
                    
                    Aha——会心一击
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="7.1.有条件创建.html">
            
                <a href="7.1.有条件创建.html">
            
                    
                        <b>1.8.1.</b>
                    
                    有条件创建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="7.2.依赖反转.html">
            
                <a href="7.2.依赖反转.html">
            
                    
                        <b>1.8.2.</b>
                    
                    依赖反转
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="7.3.多可用区分布.html">
            
                <a href="7.3.多可用区分布.html">
            
                    
                        <b>1.8.3.</b>
                    
                    多可用区分布
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="7.4.provisioner与user_data.html">
            
                <a href="7.4.provisioner与user_data.html">
            
                    
                        <b>1.8.4.</b>
                    
                    provisioner与user_data
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="7.5.destroy-provisioner中使用变量.html">
            
                <a href="7.5.destroy-provisioner中使用变量.html">
            
                    
                        <b>1.8.5.</b>
                    
                    destroy-provisioner中使用变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="7.6.利用null_resource的triggers触发其他资源更新.html">
            
                <a href="7.6.利用null_resource的triggers触发其他资源更新.html">
            
                    
                        <b>1.8.6.</b>
                    
                    利用null_resource的triggers触发其他资源更新
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="7.7.利用null_resource搭配replace_triggered_by更新无法读取的属性.html">
            
                <a href="7.7.利用null_resource搭配replace_triggered_by更新无法读取的属性.html">
            
                    
                        <b>1.8.7.</b>
                    
                    利用 null_resource 搭配 replace_triggered_by 更新无法从服务端读取内容的属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="7.8.创建资源的条件依赖另一个资源的输出时怎么办.html">
            
                <a href="7.8.创建资源的条件依赖另一个资源的输出时怎么办.html">
            
                    
                        <b>1.8.8.</b>
                    
                    创建资源的条件依赖另一个资源的输出时怎么办
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="7.9.利用create_before_destroy调整资源update执行的顺序.html">
            
                <a href="7.9.利用create_before_destroy调整资源update执行的顺序.html">
            
                    
                        <b>1.8.9.</b>
                    
                    利用 create_before_destroy 调整资源 Update 的执行顺序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.10" data-path="7.10.terraform与自动化.html">
            
                <a href="7.10.terraform与自动化.html">
            
                    
                        <b>1.8.10.</b>
                    
                    Terraform 与自动化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="8.CloudPosse最佳实践.html">
            
                <a href="8.CloudPosse最佳实践.html">
            
                    
                        <b>1.9.</b>
                    
                    来自CloudPosse的Terraform最佳实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="9.后记.html">
            
                <a href="9.后记.html">
            
                    
                        <b>1.10.</b>
                    
                    后记
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Terraform Test</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x6D4B;&#x8BD5;"><b>1.7.1. </b>&#x6D4B;&#x8BD5;</a></li><ul><li><span class="title-icon "></span><a href="#&#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#x6216;&#x5355;&#x5143;&#x6D4B;&#x8BD5;"><b>1.7.1.1. </b>&#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#x6216;&#x5355;&#x5143;&#x6D4B;&#x8BD5;</a></li><li><span class="title-icon "></span><a href="#&#x8BED;&#x6CD5;"><b>1.7.1.2. </b>&#x8BED;&#x6CD5;</a></li><ul><li><span class="title-icon "></span><a href="#&#x793A;&#x4F8B;"><b>1.7.1.2.1. </b>&#x793A;&#x4F8B;</a></li></ul><li><span class="title-icon "></span><a href="#run-&#x5757;"><b>1.7.1.3. </b>run &#x5757;</a></li><ul><li><span class="title-icon "></span><a href="#&#x65AD;&#x8A00;"><b>1.7.1.3.1. </b>&#x65AD;&#x8A00;</a></li></ul><li><span class="title-icon "></span><a href="#variable-&#x5757;"><b>1.7.1.4. </b>variable &#x5757;</a></li><ul><li><span class="title-icon "></span><a href="#&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x6216;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x53D8;&#x91CF;"><b>1.7.1.4.1. </b>&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x6216;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x53D8;&#x91CF;</a></li><li><span class="title-icon "></span><a href="#&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x4F18;&#x5148;&#x7EA7;"><b>1.7.1.4.2. </b>&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x4F18;&#x5148;&#x7EA7;</a></li><li><span class="title-icon "></span><a href="#&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x5F15;&#x7528;"><b>1.7.1.4.3. </b>&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x5F15;&#x7528;</a></li></ul><li><span class="title-icon "></span><a href="#provider-&#x5757;"><b>1.7.1.5. </b>provider &#x5757;</a></li><li><span class="title-icon "></span><a href="#module-&#x5757;"><b>1.7.1.6. </b>module &#x5757;</a></li><ul><li><span class="title-icon "></span><a href="#&#x6A21;&#x5757;&#x72B6;&#x6001;"><b>1.7.1.6.1. </b>&#x6A21;&#x5757;&#x72B6;&#x6001;</a></li></ul><li><span class="title-icon "></span><a href="#&#x9884;&#x671F;&#x5931;&#x8D25;"><b>1.7.1.7. </b>&#x9884;&#x671F;&#x5931;&#x8D25;</a></li></ul></ul></div><a href="#&#x6D4B;&#x8BD5;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x6D4B;&#x8BD5;"><a name="&#x6D4B;&#x8BD5;" class="anchor-navigation-ex-anchor" href="#&#x6D4B;&#x8BD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1. &#x6D4B;&#x8BD5;</h1>
<p>-&gt; <strong>&#x6CE8;&#x610F;&#xFF1A;</strong> &#x8BE5;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x5728; Terraform v1.6.0 &#x53CA;&#x4EE5;&#x540E;&#x7248;&#x672C;&#x4E2D;&#x53EF;&#x7528;&#x3002;</p>
<p>Terraform &#x6D4B;&#x8BD5;&#x529F;&#x80FD;&#x5141;&#x8BB8;&#x6A21;&#x5757;&#x4F5C;&#x8005;&#x9A8C;&#x8BC1;&#x914D;&#x7F6E;&#x53D8;&#x66F4;&#x4E0D;&#x4F1A;&#x5F15;&#x5165;&#x7834;&#x574F;&#x6027;&#x66F4;&#x6539;&#x3002;&#x6D4B;&#x8BD5;&#x9488;&#x5BF9;&#x7279;&#x5B9A;&#x7684;&#x3001;&#x4E34;&#x65F6;&#x7684;&#x8D44;&#x6E90;&#x8FDB;&#x884C;&#xFF0C;&#x9632;&#x6B62;&#x5BF9;&#x73B0;&#x6709;&#x7684;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#x6216;&#x72B6;&#x6001;&#x4EA7;&#x751F;&#x4EFB;&#x4F55;&#x98CE;&#x9669;&#x3002;</p>
<h2 id="&#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#x6216;&#x5355;&#x5143;&#x6D4B;&#x8BD5;"><a name="&#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#x6216;&#x5355;&#x5143;&#x6D4B;&#x8BD5;" class="anchor-navigation-ex-anchor" href="#&#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#x6216;&#x5355;&#x5143;&#x6D4B;&#x8BD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.1. &#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#x6216;&#x5355;&#x5143;&#x6D4B;&#x8BD5;</h2>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Terraform &#x6D4B;&#x8BD5;&#x4F1A;&#x521B;&#x5EFA;&#x771F;&#x5B9E;&#x7684;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#xFF0C;&#x5E76;&#x53EF;&#x4EE5;&#x5BF9;&#x8FD9;&#x4E9B;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#x8FDB;&#x884C;&#x65AD;&#x8A00;&#x548C;&#x9A8C;&#x8BC1;&#x3002;&#x8FD9;&#x76F8;&#x5F53;&#x4E8E;&#x96C6;&#x6210;&#x6D4B;&#x8BD5;&#xFF0C;&#x5B83;&#x901A;&#x8FC7;&#x8C03;&#x7528; Terraform &#x521B;&#x5EFA;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#x5E76;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x9A8C;&#x8BC1;&#x6765;&#x6D4B;&#x8BD5; Terraform &#x7684;&#x6838;&#x5FC3;&#x529F;&#x80FD;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x66F4;&#x65B0; <a href="#run-&#x5757;"><code>run</code></a> &#x5757;&#x4E2D;&#x7684; <code>command</code> &#x5C5E;&#x6027;&#xFF08;&#x4E0B;&#x9762;&#x6709;&#x793A;&#x4F8B;&#xFF09;&#x6765;&#x8986;&#x76D6;&#x9ED8;&#x8BA4;&#x7684;&#x6D4B;&#x8BD5;&#x884C;&#x4E3A;&#x3002;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x90FD;&#x4F1A;&#x6267;&#x884C; <code>command = apply</code>&#xFF0C;&#x547D;&#x4EE4; Terraform &#x5BF9;&#x4F60;&#x7684;&#x914D;&#x7F6E;&#x6267;&#x884C;&#x5B8C;&#x6574;&#x7684; <code>apply</code> &#x64CD;&#x4F5C;&#x3002;&#x5C06; <code>command</code> &#x503C;&#x66FF;&#x6362;&#x4E3A; <code>command = plan</code> &#x4F1A;&#x544A;&#x8BC9; Terraform &#x4E0D;&#x4E3A;&#x8FD9;&#x4E2A; <code>run</code> &#x5757;&#x521B;&#x5EFA;&#x65B0;&#x7684;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#x3002;&#x8FD9;&#x5C06;&#x5141;&#x8BB8;&#x6D4B;&#x8BD5;&#x4F5C;&#x8005;&#x9A8C;&#x8BC1;&#x4ED6;&#x4EEC;&#x7684;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#x4E2D;&#x7684;&#x903B;&#x8F91;&#x64CD;&#x4F5C;&#x548C;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x7F16;&#x5199;&#x4E86;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x3002;</p>
<p>Terraform v1.7.0 &#x5F15;&#x5165;&#x4E86;&#x5728; <code>terraform test</code> &#x6267;&#x884C;&#x671F;&#x95F4;&#x6A21;&#x62DF; Provider &#x8FD4;&#x56DE;&#x6570;&#x636E;&#x7684;&#x80FD;&#x529B;&#x3002;&#x8FD9;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x7F16;&#x5199;&#x66F4;&#x8BE6;&#x7EC6;&#x548C;&#x5B8C;&#x6574;&#x7684;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x3002;</p>
<h2 id="&#x8BED;&#x6CD5;"><a name="&#x8BED;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#&#x8BED;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.2. &#x8BED;&#x6CD5;</h2>
<p>&#x6BCF;&#x4E2A; Terraform &#x6D4B;&#x8BD5;&#x90FD;&#x4FDD;&#x5B58;&#x5728;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x3002;Terraform &#x6839;&#x636E;&#x6587;&#x4EF6;&#x6269;&#x5C55;&#x540D;&#x53D1;&#x73B0;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#xFF1A;<code>.tftest.hcl</code> &#x6216; <code>.tftest.json</code>&#x3002;</p>
<p>&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x5305;&#x542B;&#x4EE5;&#x4E0B;&#x6839;&#x7EA7;&#x522B;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x5757;&#xFF1A;</p>
<ul>
<li>&#x4E00;&#x4E2A;&#x5230;&#x591A;&#x4E2A; <a href="#run-&#x5757;"><code>run</code></a> &#x5757;&#x3002;</li>
<li>&#x96F6;&#x4E2A;&#x5230;&#x4E00;&#x4E2A; <a href="#variables"><code>variables</code></a> &#x5757;&#x3002;</li>
<li>&#x96F6;&#x4E2A;&#x5230;&#x591A;&#x4E2A; <a href="#providers"><code>provider</code></a> &#x5757;&#x3002;</li>
</ul>
<p>Terraform &#x6309;&#x987A;&#x5E8F;&#x6267;&#x884C; <code>run</code> &#x5757;&#xFF0C;&#x6A21;&#x62DF;&#x4E00;&#x7CFB;&#x5217;&#x76F4;&#x63A5;&#x5728;&#x914D;&#x7F6E;&#x76EE;&#x5F55;&#x4E2D;&#x6267;&#x884C;&#x7684; Terraform &#x547D;&#x4EE4;&#x3002; <code>variables</code> &#x548C; <code>provider</code> &#x5757;&#x7684;&#x987A;&#x5E8F;&#x5E76;&#x4E0D;&#x91CD;&#x8981;&#xFF0C;Terraform &#x5728;&#x6D4B;&#x8BD5;&#x64CD;&#x4F5C;&#x5F00;&#x59CB;&#x65F6;&#x5904;&#x7406;&#x8FD9;&#x4E9B;&#x5757;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x503C;&#x3002;&#x6211;&#x4EEC;&#x5EFA;&#x8BAE;&#x9996;&#x5148;&#x5728;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x7684;&#x5F00;&#x5934;&#x5B9A;&#x4E49;&#x4F60;&#x7684; <code>variables</code> &#x548C; <code>provider</code> &#x5757;&#x3002;</p>
<h3 id="&#x793A;&#x4F8B;"><a name="&#x793A;&#x4F8B;" class="anchor-navigation-ex-anchor" href="#&#x793A;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.2.1. &#x793A;&#x4F8B;</h3>
<p>&#x4EE5;&#x4E0B;&#x793A;&#x4F8B;&#x6F14;&#x793A;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684; Terraform &#x914D;&#x7F6E;&#xFF0C;&#x8BE5;&#x914D;&#x7F6E;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A; AWS S3 &#x5B58;&#x50A8;&#x6876;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x8F93;&#x5165;&#x53D8;&#x91CF;&#x6765;&#x4FEE;&#x6539;&#x5176;&#x540D;&#x79F0;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#xFF08;&#x5982;&#x4E0B;&#xFF09;&#x6765;&#x9A8C;&#x8BC1;&#x5B58;&#x50A8;&#x6876;&#x7684;&#x540D;&#x79F0;&#x662F;&#x5426;&#x5982;&#x9884;&#x671F;&#x90A3;&#x6837;&#x88AB;&#x521B;&#x5EFA;&#x3002;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># main.tf</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
    <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;eu-central-1&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">variable<span class="token type variable"> &quot;bucket_prefix&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">type</span> <span class="token punctuation">=</span> string
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> <span class="token string">&quot;<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">var</span><span class="token punctuation">.</span><span class="token type variable">bucket_prefix</span><span class="token punctuation">}</span></span>-bucket&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">output<span class="token type variable"> &quot;bucket_name&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">value</span> <span class="token punctuation">=</span> aws_s3_bucket.bucket.bucket
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x8FD0;&#x884C;&#x4E86;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x7684;Terraform <code>plan</code> &#x547D;&#x4EE4;&#xFF0C;&#x8BE5;&#x547D;&#x4EE4;&#x521B;&#x5EFA;&#x4E86;S3&#x5B58;&#x50A8;&#x6876;&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7;&#x68C0;&#x67E5;&#x5B9E;&#x9645;&#x540D;&#x79F0;&#x662F;&#x5426;&#x4E0E;&#x9884;&#x671F;&#x540D;&#x79F0;&#x5339;&#x914D;&#xFF0C;&#x6765;&#x9A8C;&#x8BC1;&#x8BA1;&#x7B97;&#x540D;&#x79F0;&#x7684;&#x903B;&#x8F91;&#x662F;&#x5426;&#x6B63;&#x786E;&#x3002;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># valid_string_concat.tftest.hcl</span>

<span class="token keyword">variables</span> <span class="token punctuation">{</span>
  <span class="token property">bucket_prefix</span> <span class="token punctuation">=</span> <span class="token string">&quot;test&quot;</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;valid_string_concat&quot;</span> <span class="token punctuation">{</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-bucket&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;S3 bucket name did not match expected&quot;</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<h2 id="run-&#x5757;"><a name="run-&#x5757;" class="anchor-navigation-ex-anchor" href="#run-&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.3. run &#x5757;</h2>
<p>&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x90FD;&#x6709;&#x4EE5;&#x4E0B;&#x5B57;&#x6BB5;&#x548C;&#x5757;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>&#x5B57;&#x6BB5;&#x6216;&#x5757;&#x540D;&#x79F0;</th>
<th>&#x63CF;&#x8FF0;</th>
<th>&#x9ED8;&#x8BA4;&#x503C;</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>command</code></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x662F; <code>apply</code> &#x6216; <code>plan</code>&#x3002;</td>
<td><code>apply</code></td>
</tr>
<tr>
<td><code>plan_options.mode</code></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x662F; <code>normal</code> &#x6216; <code>refresh-only</code>&#x3002;</td>
<td><code>normal</code></td>
</tr>
<tr>
<td><code>plan_options.refresh</code></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684; <code>bool</code> &#x5C5E;&#x6027;&#x3002;</td>
<td><code>true</code></td>
</tr>
<tr>
<td><code>plan_options.replace</code></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF0C;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x8D44;&#x6E90;&#x5730;&#x5740;&#x5217;&#x8868;&#xFF0C;&#x5F15;&#x7528;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;&#x4E2D;&#x7684;&#x8D44;&#x6E90;&#x3002;</td>
<td></td>
</tr>
<tr>
<td><code>plan_options.target</code></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF0C;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x8D44;&#x6E90;&#x5730;&#x5740;&#x5217;&#x8868;&#xFF0C;&#x5F15;&#x7528;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;&#x4E2D;&#x7684;&#x8D44;&#x6E90;&#x3002;</td>
<td></td>
</tr>
<tr>
<td><a href="#variable-&#x5757;"><code>variables</code></a></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684; <code>variables</code> &#x5757;&#x3002;</td>
<td></td>
</tr>
<tr>
<td><a href="#module-&#x5757;"><code>module</code></a></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684; <code>module</code> &#x5757;&#x3002;</td>
<td></td>
</tr>
<tr>
<td><a href="#provider-&#x5757;"><code>providers</code></a></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684; <code>providers</code> &#x5C5E;&#x6027;&#x3002;</td>
<td></td>
</tr>
<tr>
<td><a href="#assertion-&#x5757;"><code>assert</code></a></td>
<td>&#x53EF;&#x9009;&#x7684; <code>assert</code> &#x5757;&#x3002;</td>
<td></td>
</tr>
<tr>
<td><code>expect_failures</code></td>
<td>&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#x3002;</td>
</tr>
</tbody>
</table>
<p><code>command</code> &#x5C5E;&#x6027;&#x548C; <code>plan_options</code> &#x5757;&#x544A;&#x8BC9; Terraform &#x5BF9;&#x4E8E;&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x6267;&#x884C;&#x54EA;&#x4E2A;&#x547D;&#x4EE4;&#x548C;&#x9009;&#x9879;&#x3002;&#x5982;&#x679C;&#x60A8;&#x6CA1;&#x6709;&#x6307;&#x5B9A; <code>command</code> &#x5C5E;&#x6027;&#x6216; <code>plan_options</code> &#x5757;&#xFF0C;&#x90A3;&#x4E48;&#x9ED8;&#x8BA4;&#x64CD;&#x4F5C;&#x662F;&#x666E;&#x901A;&#x7684; <code>terraform apply</code> &#x64CD;&#x4F5C;&#x3002;</p>
<p><code>command</code> &#x5C5E;&#x6027;&#x6307;&#x660E;&#x64CD;&#x4F5C;&#x5E94;&#x8BE5;&#x662F;&#x4E00;&#x4E2A; <a href="5.14.plan.html"><code>plan</code></a> &#x64CD;&#x4F5C;&#x8FD8;&#x662F;&#x4E00;&#x4E2A; <a href="5.4.apply.html"><code>apply</code></a> &#x64CD;&#x4F5C;&#x3002;</p>
<p><code>plan_options</code> &#x5757;&#x5141;&#x8BB8;&#x6D4B;&#x8BD5;&#x7684;&#x4F5C;&#x8005;&#x5B9A;&#x4E49;&#x4ED6;&#x4EEC;&#x901A;&#x5E38;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x6807;&#x5FD7;&#x548C;&#x9009;&#x9879;&#x5B9A;&#x4E49;&#x7684; <a href="https://developer.hashicorp.com/terraform/cli/commands/plan#planning-modes" target="_blank">plan mode</a> &#x548C; <a href="https://developer.hashicorp.com/terraform/cli/commands/plan#planning-options" target="_blank">&#x9009;&#x9879;</a>&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5728; <a href="#variable-&#x5757;">&#x53D8;&#x91CF;</a> &#x90E8;&#x5206;&#x4ECB;&#x7ECD; <code>-var</code> &#x548C; <code>-var-file</code> &#x9009;&#x9879;&#x3002;</p>
<h3 id="&#x65AD;&#x8A00;"><a name="&#x65AD;&#x8A00;" class="anchor-navigation-ex-anchor" href="#&#x65AD;&#x8A00;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.3.1. &#x65AD;&#x8A00;</h3>
<p>Terraform &#x6D4B;&#x8BD5;&#x7684; <code>run</code> &#x5757;&#x65AD;&#x8A00;&#x662F;<a href="https://developer.hashicorp.com/terraform/language/expressions/custom-conditions" target="_blank">&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;</a>&#xFF0C;&#x7531;<a href="https://developer.hashicorp.com/terraform/language/expressions/custom-conditions" target="_blank">&#x6761;&#x4EF6;</a>&#x548C;<a href="https://developer.hashicorp.com/terraform/language/expressions/custom-conditions#error-messages" target="_blank">&#x9519;&#x8BEF;&#x6D88;&#x606F;</a>&#x7EC4;&#x6210;&#x3002;</p>
<p>&#x5728; Terraform &#x6D4B;&#x8BD5;&#x547D;&#x4EE4;&#x6267;&#x884C;&#x7ED3;&#x675F;&#x65F6;&#xFF0C;Terraform &#x4F1A;&#x5C06;&#x6240;&#x6709;&#x5931;&#x8D25;&#x7684;&#x65AD;&#x8A00;&#x4F5C;&#x4E3A;&#x6D4B;&#x8BD5;&#x901A;&#x8FC7;&#x6216;&#x5931;&#x8D25;&#x72B6;&#x6001;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x5C55;&#x793A;&#x51FA;&#x6765;&#x3002;</p>
<h4 id="&#x65AD;&#x8A00;&#x4E2D;&#x7684;&#x5F15;&#x7528;"><a name="&#x65AD;&#x8A00;&#x4E2D;&#x7684;&#x5F15;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x65AD;&#x8A00;&#x4E2D;&#x7684;&#x5F15;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x65AD;&#x8A00;&#x4E2D;&#x7684;&#x5F15;&#x7528;</h4>
<p>&#x6D4B;&#x8BD5;&#x4E2D;&#x7684;&#x65AD;&#x8A00;&#x53EF;&#x4EE5;&#x5F15;&#x7528;&#x4E3B; Terraform &#x914D;&#x7F6E;&#x4E2D;&#x7684;&#x5176;&#x4ED6;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x53EF;&#x7528;&#x7684;&#x4EFB;&#x4F55;&#x73B0;&#x6709;<a href="3.8.&#x8868;&#x8FBE;&#x5F0F;.html#&#x5F15;&#x7528;&#x547D;&#x540D;&#x503C;">&#x547D;&#x540D;&#x503C;</a>&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;&#x6D4B;&#x8BD5;&#x65AD;&#x8A00;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5F15;&#x7528;&#x5F53;&#x524D;&#x548C;&#x5148;&#x524D; <code>run</code> &#x5757;&#x7684;&#x8F93;&#x51FA;&#x3002;&#x6BD4;&#x5982;&#x5F15;&#x7528;&#x4E86;<a href="#&#x793A;&#x4F8B;">&#x4E0A;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;</a>&#x4E2D;&#x7684;&#x8F93;&#x51FA;&#x7684;&#x4E00;&#x4E2A;&#x5408;&#x6CD5;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x6761;&#x4EF6;&#xFF1A;<code>condition = output.bucket_name == &quot;test_bucket&quot;</code>&#x3002;</p>
<h2 id="variable-&#x5757;"><a name="variable-&#x5757;" class="anchor-navigation-ex-anchor" href="#variable-&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.4. variable &#x5757;</h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;&#x4F60;&#x7684;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x4E3A; <a href="3.3.&#x8F93;&#x5165;&#x53D8;&#x91CF;.html">&#x8F93;&#x5165;&#x53D8;&#x91CF;</a> &#x8BBE;&#x7F6E;&#x503C;&#x3002;</p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x7684;&#x6839;&#x7EA7;&#x522B;&#x6216;&#x8005; <code>run</code> &#x5757;&#x5185;&#x90E8;&#x5B9A;&#x4E49; <code>variables</code> &#x5757;&#x3002;Terraform &#x5C06;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x53D8;&#x91CF;&#x503C;&#x4F20;&#x9012;&#x5230;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x6240;&#x6709; <code>run</code> &#x5757;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5728;&#x67D0;&#x4E2A; <code>run</code> &#x5757;&#x4E2D;&#x76F4;&#x63A5;&#x8BBE;&#x7F6E;&#x53D8;&#x91CF;&#x503C;&#x6765;&#x8986;&#x76D6;&#x4ECE;&#x6839;&#x90E8;&#x7EE7;&#x627F;&#x7684;&#x503C;&#x3002;</p>
<p>&#x5728;&#x4E0A;&#x8FF0; <a href="#&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</a> &#x7684;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x6DFB;&#x52A0;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># variable_precedence.tftest.hcl</span>

<span class="token keyword">variables</span> <span class="token punctuation">{</span>
  <span class="token property">bucket_prefix</span> <span class="token punctuation">=</span> <span class="token string">&quot;test&quot;</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;uses_root_level_value&quot;</span> <span class="token punctuation">{</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-bucket&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;S3 bucket name did not match expected&quot;</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>

run <span class="token string">&quot;overrides_root_level_value&quot;</span> <span class="token punctuation">{</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token keyword">variables</span> <span class="token punctuation">{</span>
    <span class="token property">bucket_prefix</span> <span class="token punctuation">=</span> <span class="token string">&quot;other&quot;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;other-bucket&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;S3 bucket name did not match expected&quot;</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86;&#x7B2C;&#x4E8C;&#x4E2A; <code>run</code> &#x5757;&#xFF0C;&#x8BE5;&#x5757;&#x6307;&#x5B9A; <code>bucket_prefix</code> &#x53D8;&#x91CF;&#x503C;&#x4E3A; <code>other</code>&#xFF0C;&#x8986;&#x76D6;&#x4E86;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x63D0;&#x4F9B;&#x7684;&#xFF0C;&#x5E76;&#x5728;&#x7B2C;&#x4E00;&#x4E2A; <code>run</code> &#x5757;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x503C; &#x2014;&#x2014; <code>test</code>&#x3002;</p>
<h3 id="&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x6216;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x53D8;&#x91CF;"><a name="&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x6216;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x53D8;&#x91CF;" class="anchor-navigation-ex-anchor" href="#&#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x6216;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x53D8;&#x91CF;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.4.1. &#x901A;&#x8FC7;&#x547D;&#x4EE4;&#x884C;&#x6216;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x53D8;&#x91CF;</h3>
<p>&#x9664;&#x4E86;&#x901A;&#x8FC7;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x53D8;&#x91CF;&#x503C;&#x5916;&#xFF0C;Terraform <code>test</code> &#x547D;&#x4EE4;&#x8FD8;&#x652F;&#x6301;&#x6307;&#x5B9A;&#x53D8;&#x91CF;&#x503C;&#x7684;&#x5176;&#x4ED6;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x60A8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <a href="3.3.&#x8F93;&#x5165;&#x53D8;&#x91CF;.html#&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;">&#x547D;&#x4EE4;&#x884C;</a> &#x548C; <a href="3.3.&#x8F93;&#x5165;&#x53D8;&#x91CF;.html#&#x53C2;&#x6570;&#x6587;&#x4EF6;">&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x6587;&#x4EF6;</a> &#x4E3A;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x6307;&#x5B9A;&#x53D8;&#x91CF;&#x503C;&#x3002;</p>
<p>&#x50CF;&#x666E;&#x901A;&#x7684; Terraform &#x547D;&#x4EE4;&#x4E00;&#x6837;&#xFF0C;Terraform &#x4F1A;&#x81EA;&#x52A8;&#x52A0;&#x8F7D;&#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x4EFB;&#x4F55;&#x53D8;&#x91CF;&#x6587;&#x4EF6;&#x3002;&#x81EA;&#x52A8;&#x53D8;&#x91CF;&#x6587;&#x4EF6;&#x5305;&#x62EC; <code>terraform.tfvars</code>&#x3001;<code>terraform.tfvars.json</code>&#xFF0C;&#x4EE5;&#x53CA;&#x6240;&#x6709;&#x4EE5; <code>.auto.tfvars</code> &#x6216; <code>.auto.tfvars.json</code> &#x7ED3;&#x5C3E;&#x7684;&#x6587;&#x4EF6;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong> &#x4ECE;&#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x4E2D;&#x7684;&#x81EA;&#x52A8;&#x53D8;&#x91CF;&#x6587;&#x4EF6;&#x52A0;&#x8F7D;&#x7684;&#x53D8;&#x91CF;&#x503C;&#x53EA;&#x9002;&#x7528;&#x4E8E;&#x5728;&#x540C;&#x4E00;&#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x6D4B;&#x8BD5;&#x3002;&#x4EE5;&#x6240;&#x6709;&#x5176;&#x4ED6;&#x65B9;&#x5F0F;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x5C06;&#x9002;&#x7528;&#x4E8E;&#x7ED9;&#x5B9A;&#x6D4B;&#x8BD5;&#x8FD0;&#x884C;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x6D4B;&#x8BD5;&#x3002;</p>
<p>&#x8FD9;&#x5728;&#x4F7F;&#x7528;&#x654F;&#x611F;&#x53D8;&#x91CF;&#x503C;&#x548C;&#x8BBE;&#x7F6E; Provider &#x914D;&#x7F6E;&#x65F6;&#x7279;&#x522B;&#x6709;&#x7528;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x53EF;&#x80FD;&#x4F1A;&#x76F4;&#x63A5;&#x66B4;&#x9732;&#x8FD9;&#x4E9B;&#x654F;&#x611F;&#x503C;&#x3002;</p>
<h3 id="&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x4F18;&#x5148;&#x7EA7;"><a name="&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x4F18;&#x5148;&#x7EA7;" class="anchor-navigation-ex-anchor" href="#&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x4F18;&#x5148;&#x7EA7;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.4.2. &#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x4F18;&#x5148;&#x7EA7;</h3>
<p>&#x9664;&#x4E86;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x8BBE;&#x7F6E;&#x7684;&#x53D8;&#x91CF;&#x503C;&#xFF0C;<a href="3.3.&#x8F93;&#x5165;&#x53D8;&#x91CF;.html#&#x8F93;&#x5165;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#x4F18;&#x5148;&#x7EA7;">&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x4F18;&#x5148;&#x7EA7;</a> &#x5728;&#x6D4B;&#x8BD5;&#x4E2D;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;&#x3002;&#x5728;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x5177;&#x6709;&#x6700;&#x9AD8;&#x4F18;&#x5148;&#x7EA7;&#xFF0C;&#x53EF;&#x4EE5;&#x8986;&#x76D6;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x3001;&#x53D8;&#x91CF;&#x6587;&#x4EF6;&#x6216;&#x547D;&#x4EE4;&#x884C;&#x8F93;&#x5165;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;&#x5728;&#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x6D4B;&#x8BD5;&#xFF0C;&#x4EFB;&#x4F55;&#x5728;&#x6D4B;&#x8BD5;&#x76EE;&#x5F55;&#x7684;&#x81EA;&#x52A8;&#x53D8;&#x91CF;&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x503C;&#x90FD;&#x5C06;&#x8986;&#x76D6;&#x4E3B;&#x914D;&#x7F6E;&#x76EE;&#x5F55;&#x7684;&#x81EA;&#x52A8;&#x53D8;&#x91CF;&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x503C;&#x3002;</p>
<h3 id="&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x5F15;&#x7528;"><a name="&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x5F15;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x5F15;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.4.3. &#x53D8;&#x91CF;&#x4E2D;&#x7684;&#x5F15;&#x7528;</h3>
<p>&#x5728; <code>run</code> &#x5757;&#x4E2D;&#x5B9A;&#x4E49;&#x7684; <code>variable</code> &#x4E2D;&#x53EF;&#x4EE5;&#x5F15;&#x7528;&#x5728;&#x5148;&#x524D; <code>run</code> &#x5757;&#x4E2D;&#x6267;&#x884C;&#x7684;&#x6A21;&#x5757;&#x7684;&#x8F93;&#x51FA;&#x548C;&#x5728;&#x66F4;&#x9AD8;&#x4F18;&#x5148;&#x7EA7;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x4EE5;&#x4E0B;&#x4EE3;&#x7801;&#x5757;&#x663E;&#x793A;&#x4E86;&#x53D8;&#x91CF;&#x5982;&#x4F55;&#x5F15;&#x7528;&#x66F4;&#x9AD8;&#x4F18;&#x5148;&#x7EA7;&#x7684;&#x53D8;&#x91CF;&#x548C;&#x5148;&#x524D;&#x7684; <code>run</code> &#x5757;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token keyword">variables</span> <span class="token punctuation">{</span>
  <span class="token property">global_value</span> <span class="token punctuation">=</span> <span class="token string">&quot;some value&quot;</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;run_block_one&quot;</span> <span class="token punctuation">{</span>
  <span class="token keyword">variables</span> <span class="token punctuation">{</span>
    <span class="token property">local_value</span> <span class="token punctuation">=</span> var.global_value
  <span class="token punctuation">}</span>

  <span class="token comment"># ...</span>
  <span class="token comment"># &#x8FD9;&#x91CC;&#x5E94;&#x8BE5;&#x6709;&#x4E00;&#x4E9B;&#x6D4B;&#x8BD5;&#x65AD;&#x8A00;</span>
  <span class="token comment"># ...</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;run_block_two&quot;</span> <span class="token punctuation">{</span>
  <span class="token keyword">variables</span> <span class="token punctuation">{</span>
    <span class="token property">local_value</span> <span class="token punctuation">=</span> run.run_block_one.output_one
  <span class="token punctuation">}</span>

  <span class="token comment"># ...</span>
  <span class="token comment"># &#x8FD9;&#x91CC;&#x5E94;&#x8BE5;&#x6709;&#x4E00;&#x4E9B;&#x6D4B;&#x8BD5;&#x65AD;&#x8A00;</span>
  <span class="token comment"># ...</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#xFF0C;<code>run_block_one</code> &#x4E2D;&#x7684; <code>local_value</code> &#x4ECE; <code>global_value</code> &#x53D8;&#x91CF;&#x83B7;&#x53D6;&#x503C;&#x3002;&#x5982;&#x679C;&#x4F60;&#x60F3;&#x7ED9;&#x591A;&#x4E2A;&#x53D8;&#x91CF;&#x5206;&#x914D;&#x76F8;&#x540C;&#x7684;&#x503C;&#xFF0C;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x5F88;&#x6709;&#x7528;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x6587;&#x4EF6;&#x7EA7;&#x522B;&#x4E00;&#x6B21;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x503C;&#xFF0C;&#x7136;&#x540E;&#x4E0E;&#x4E0D;&#x540C;&#x7684;&#x53D8;&#x91CF;&#x5171;&#x4EAB;&#x5B83;&#x3002;</p>
<p>&#x76F8;&#x6BD4;&#x4E4B;&#x4E0B;&#xFF0C;<code>run_block_two</code> &#x4E2D;&#x7684; <code>local_value</code> &#x5F15;&#x7528;&#x4E86; <code>run_block_one</code> &#x7684; <code>output_one</code> &#x7684;&#x8F93;&#x51FA;&#x503C;&#x3002;&#x8FD9;&#x79CD;&#x6A21;&#x5F0F;&#x5BF9;&#x4E8E;&#x5728; <code>run</code> &#x5757;&#x4E4B;&#x95F4;&#x4F20;&#x9012;&#x503C;&#x7279;&#x522B;&#x6709;&#x7528;&#xFF0C;&#x7279;&#x522B;&#x662F;&#x5982;&#x679C; <code>run</code> &#x5757;&#x6B63;&#x5728;&#x6267;&#x884C;<a href="#module-&#x5757;">&#x6A21;&#x5757;</a>&#x90E8;&#x5206;&#x4E2D;&#x8BE6;&#x7EC6;&#x63CF;&#x8FF0;&#x7684;&#x4E0D;&#x540C;&#x6A21;&#x5757;&#x3002;</p>
<h2 id="provider-&#x5757;"><a name="provider-&#x5757;" class="anchor-navigation-ex-anchor" href="#provider-&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.5. provider &#x5757;</h2>
<p>&#x60A8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <code>provider</code> &#x548C; <code>providers</code> &#x5757;&#x548C;&#x5C5E;&#x6027;&#xFF0C;&#x5728;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x8BBE;&#x7F6E;&#x6216;&#x8986;&#x76D6; Terraform &#x4EE3;&#x7801;&#x6240;&#x9700;&#x7684; Provider&#x3002;</p>
<p>&#x60A8;&#x53EF;&#x4EE5;&#x5728; Terraform &#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x7684;&#x6839;&#x7EA7;&#x522B;&#xFF0C;&#x5B9A;&#x4E49; <a href="https://developer.hashicorp.com/terraform/language/providers/configuration" target="_blank"><code>provider</code> &#x5757;</a>&#xFF0C;&#x5C31;&#x50CF;&#x5728; Terraform <a href="https://developer.hashicorp.com/terraform/language/providers" target="_blank">&#x914D;&#x7F6E;&#x4EE3;&#x7801;&#x4E2D;</a>&#x521B;&#x5EFA;&#x5B83;&#x4EEC;&#x4E00;&#x6837;&#x3002;&#x7136;&#x540E;&#xFF0C;Terraform &#x4F1A;&#x5C06;&#x8FD9;&#x4E9B; <code>provider</code> &#x5757;&#x4F20;&#x9012;&#x5230;&#x5176;&#x914D;&#x7F6E;&#x4E2D;&#xFF0C;&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x6267;&#x884C;&#x65F6;&#x90FD;&#x662F;&#x5982;&#x6B64;&#x3002;</p>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x60A8;&#x6307;&#x5B9A;&#x7684;&#x6BCF;&#x4E2A; Provider &#x90FD;&#x76F4;&#x63A5;&#x5728;&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x4E2D;&#x53EF;&#x7528;&#x3002;&#x60A8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <code>providers</code> &#x5C5E;&#x6027;&#x5728;&#x7279;&#x5B9A; <code>run</code> &#x5757;&#x4E2D;&#x8BBE;&#x7F6E; Provider &#x7684;&#x53EF;&#x7528;&#x6027;&#x3002;&#x8FD9;&#x4E2A;&#x5757;&#x7684;&#x884C;&#x4E3A;&#x548C;&#x8BED;&#x6CD5;&#x4E0E; <a href="https://developer.hashicorp.com/terraform/language/meta-arguments/module-providers" target="_blank">providers meta-argument</a> &#x7684;&#x884C;&#x4E3A;&#x76F8;&#x5339;&#x914D;&#x3002;</p>
<p>&#x5982;&#x679C;&#x60A8;&#x5728;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x4E0D;&#x63D0;&#x4F9B; Provider &#x914D;&#x7F6E;&#xFF0C;Terraform &#x4F1A;&#x5C1D;&#x8BD5;&#x4F7F;&#x7528; Provider &#x7684;&#x9ED8;&#x8BA4;&#x8BBE;&#x7F6E;&#x521D;&#x59CB;&#x5316;&#x5176;&#x914D;&#x7F6E;&#x4E2D;&#x7684;&#x6240;&#x6709; Provider&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4EFB;&#x4F55;&#x65E8;&#x5728;&#x914D;&#x7F6E; Provider &#x7684;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4ECD;&#x7136;&#x53EF;&#x7528;&#xFF0C;&#x5E76;&#x4E14; Terraform &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x4EEC;&#x6765;&#x521B;&#x5EFA;&#x9ED8;&#x8BA4; Provider&#x3002;</p>
<p>&#x4E0B;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x6269;&#x5C55;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x7684; <a href="#&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</a>&#xFF0C;&#x7528;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x800C;&#x4E0D;&#x662F; Terraform &#x914D;&#x7F6E;&#x4EE3;&#x7801;&#x6765;&#x6307;&#x5B9A; <code>region</code>&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x6D4B;&#x8BD5;&#x4EE5;&#x4E0B;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># main.tf</span>

<span class="token keyword">terraform</span> <span class="token punctuation">{</span>
  <span class="token keyword">required_providers</span> <span class="token punctuation">{</span>
    <span class="token property">aws</span> <span class="token punctuation">=</span> <span class="token punctuation">{</span>
      <span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">&quot;hashicorp/aws&quot;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">variable<span class="token type variable"> &quot;bucket_prefix&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">type</span> <span class="token punctuation">=</span> string
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> <span class="token string">&quot;<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">var</span><span class="token punctuation">.</span><span class="token type variable">bucket_prefix</span><span class="token punctuation">}</span></span>-bucket&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">output<span class="token type variable"> &quot;bucket_name&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">value</span> <span class="token punctuation">=</span> aws_s3_bucket.bucket.bucket
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x5728;&#x4EE5;&#x4E0B;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x5982;&#x4E0B;&#x7684; <code>provider</code> &#x5757;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># customised_provider.tftest.hcl</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
    <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;eu-central-1&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">variables</span> <span class="token punctuation">{</span>
  <span class="token property">bucket_prefix</span> <span class="token punctuation">=</span> <span class="token string">&quot;test&quot;</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;valid_string_concat&quot;</span> <span class="token punctuation">{</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-bucket&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;S3 bucket name did not match expected&quot;</span>
  <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x66F4;&#x590D;&#x6742;&#x7684;&#x793A;&#x4F8B;&#x914D;&#x7F6E;&#xFF0C;&#x4F7F;&#x7528;&#x591A;&#x4E2A; Provider &#x4EE5;&#x53CA;&#x522B;&#x540D;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># main.tf</span>

<span class="token keyword">terraform</span> <span class="token punctuation">{</span>
  <span class="token keyword">required_providers</span> <span class="token punctuation">{</span>
    <span class="token property">aws</span> <span class="token punctuation">=</span> <span class="token punctuation">{</span>
      <span class="token property">source</span>                <span class="token punctuation">=</span> <span class="token string">&quot;hashicorp/aws&quot;</span>
      <span class="token property">configuration_aliases</span> <span class="token punctuation">=</span> <span class="token punctuation">[</span>aws.secondary<span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">variable<span class="token type variable"> &quot;bucket_prefix&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">default</span> <span class="token punctuation">=</span> <span class="token string">&quot;test&quot;</span>
  <span class="token property">type</span>    <span class="token punctuation">=</span> string
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;primary_bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> <span class="token string">&quot;<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">var</span><span class="token punctuation">.</span><span class="token type variable">bucket_prefix</span><span class="token punctuation">}</span></span>-primary&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;secondary_bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">provider</span> <span class="token punctuation">=</span> aws.secondary
  <span class="token property">bucket</span>   <span class="token punctuation">=</span> <span class="token string">&quot;<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">var</span><span class="token punctuation">.</span><span class="token type variable">bucket_prefix</span><span class="token punctuation">}</span></span>-secondary&quot;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5728;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8BBE;&#x5B9A;&#x591A;&#x4E2A; Provider&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># customised_providers.tftest.hcl</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;us-east-1&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">alias</span>  <span class="token punctuation">=</span> <span class="token string">&quot;secondary&quot;</span>
  <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;eu-central-1&quot;</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;providers&quot;</span> <span class="token punctuation">{</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.primary_bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-primary&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;invalid value for primary S3 bucket&quot;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.secondary_bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-secondary&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;invalid value for secondary S3 bucket&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x7279;&#x5B9A; <code>run</code> &#x5757;&#x4E2D;&#x58F0;&#x660E;&#x7279;&#x5B9A;&#x7684; Provider&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># main.tf</span>

<span class="token keyword">terraform</span> <span class="token punctuation">{</span>
  <span class="token keyword">required_providers</span> <span class="token punctuation">{</span>
    <span class="token property">aws</span> <span class="token punctuation">=</span> <span class="token punctuation">{</span>
      <span class="token property">source</span>                <span class="token punctuation">=</span> <span class="token string">&quot;hashicorp/aws&quot;</span>
      <span class="token property">configuration_aliases</span> <span class="token punctuation">=</span> <span class="token punctuation">[</span>aws.secondary<span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">data <span class="token type variable">&quot;aws_region&quot;</span></span> <span class="token string">&quot;primary&quot;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token keyword">data <span class="token type variable">&quot;aws_region&quot;</span></span> <span class="token string">&quot;secondary&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">provider</span> <span class="token punctuation">=</span> aws.secondary
<span class="token punctuation">}</span>

<span class="token keyword">variable<span class="token type variable"> &quot;bucket_prefix&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">default</span> <span class="token punctuation">=</span> <span class="token string">&quot;test&quot;</span>
  <span class="token property">type</span>    <span class="token punctuation">=</span> string
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;primary_bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> <span class="token string">&quot;<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">var</span><span class="token punctuation">.</span><span class="token type variable">bucket_prefix</span><span class="token punctuation">}</span></span>-<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">data</span><span class="token punctuation">.</span><span class="token type variable">aws_region</span><span class="token punctuation">.</span>primary<span class="token punctuation">.</span>name<span class="token punctuation">}</span></span>-primary&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;secondary_bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">provider</span> <span class="token punctuation">=</span> aws.secondary
  <span class="token property">bucket</span>   <span class="token punctuation">=</span> <span class="token string">&quot;<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">var</span><span class="token punctuation">.</span><span class="token type variable">bucket_prefix</span><span class="token punctuation">}</span></span>-<span class="token interpolation"><span class="token punctuation">$</span><span class="token punctuation">{</span><span class="token keyword">data</span><span class="token punctuation">.</span><span class="token type variable">aws_region</span><span class="token punctuation">.</span>secondary<span class="token punctuation">.</span>name<span class="token punctuation">}</span></span>-secondary&quot;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x53EF;&#x4EE5;&#x4E3A;&#x4E0D;&#x540C;&#x7684; <code>run</code> &#x5757;&#x914D;&#x7F6E;&#x7684; Provider&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># customised_providers.tftest.hcl</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;us-east-1&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">alias</span>  <span class="token punctuation">=</span> <span class="token string">&quot;secondary&quot;</span>
  <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;eu-central-1&quot;</span>
<span class="token punctuation">}</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">alias</span>  <span class="token punctuation">=</span> <span class="token string">&quot;tertiary&quot;</span>
  <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;eu-west-2&quot;</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;default_providers&quot;</span> <span class="token punctuation">{</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.primary_bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-us-east-1-primary&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;invalid value for primary S3 bucket&quot;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.secondary_bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-eu-central-1-secondary&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;invalid value for secondary S3 bucket&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;customised_providers&quot;</span> <span class="token punctuation">{</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token property">providers</span> <span class="token punctuation">=</span> <span class="token punctuation">{</span>
    <span class="token property">aws</span>           <span class="token punctuation">=</span> aws
    <span class="token property">aws.secondary</span> <span class="token punctuation">=</span> aws.tertiary
  <span class="token punctuation">}</span>

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.primary_bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-us-east-1-primary&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;invalid value for primary S3 bucket&quot;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span>     <span class="token punctuation">=</span> aws_s3_bucket.secondary_bucket.bucket <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token string">&quot;test-eu-west-2-secondary&quot;</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;invalid value for secondary S3 bucket&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong> &#x5728;&#x4F7F;&#x7528; <code>command = apply</code> &#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x65F6;&#xFF0C;<code>run</code> &#x5757;&#x4E4B;&#x95F4;&#x5207;&#x6362; Provider &#x53EF;&#x80FD;&#x4F1A;&#x5BFC;&#x81F4;&#x8FD0;&#x884C;&#x548C;&#x6D4B;&#x8BD5;&#x5931;&#x8D25;&#xFF0C;&#x56E0;&#x4E3A;&#x7531;&#x4E00;&#x4E2A; Provider &#x5B9A;&#x4E49;&#x521B;&#x5EFA;&#x7684;&#x8D44;&#x6E90;&#x5728;&#x88AB;&#x53E6;&#x4E00;&#x4E2A;&#x4FEE;&#x6539;&#x65F6;&#x5C06;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x4ECE; Terraform v1.7.0 &#x5F00;&#x59CB;&#xFF0C;<code>provider</code> &#x5757;&#x4E5F;&#x53EF;&#x4EE5;&#x5F15;&#x7528;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x53D8;&#x91CF;&#x548C; <code>run</code> &#x5757;&#x8F93;&#x51FA;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x53EF;&#x4EE5;&#x4ECE;&#x4E00;&#x4E2A; Provider &#x83B7;&#x53D6;&#x51ED;&#x8BC1;&#x548C;&#x5176;&#x4ED6;&#x8BBE;&#x7F6E;&#x4FE1;&#x606F;&#xFF0C;&#x5E76;&#x5728;&#x521D;&#x59CB;&#x5316;&#x7B2C;&#x4E8C;&#x4E2A; Provider &#x65F6;&#x4F7F;&#x7528;&#x8FD9;&#x4E9B;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x9996;&#x5148;&#x521D;&#x59CB;&#x5316; <code>vault</code> Provider&#xFF0C;&#x7136;&#x540E;&#x5728;&#x4E00;&#x4E2A;&#x8BBE;&#x7F6E;&#x6A21;&#x5757;&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#x6765;&#x63D0;&#x53D6; <code>aws</code> Provider &#x7684;&#x51ED;&#x8BC1;&#x3002;&#x6709;&#x5173; setup &#x6A21;&#x5757;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x9605; <a href="#module-&#x5757;">&#x6A21;&#x5757;</a>&#x3002;</p>
<pre class="language-"><code class="lang-hcl">
<span class="token keyword">provider<span class="token type variable"> &quot;vault&quot; </span></span><span class="token punctuation">{</span>
  <span class="token comment"># ... vault configuration ...</span>
<span class="token punctuation">}</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">region</span>     <span class="token punctuation">=</span> <span class="token string">&quot;us-east-1&quot;</span>

  <span class="token comment"># The `aws` provider can reference the outputs of the &quot;vault_setup&quot; run block.</span>
  <span class="token property">access_key</span> <span class="token punctuation">=</span> run.vault_setup.aws_access_key
  <span class="token property">secret_key</span> <span class="token punctuation">=</span> run.vault_setup.aws_secret_key
<span class="token punctuation">}</span>

run <span class="token string">&quot;vault_setup&quot;</span> <span class="token punctuation">{</span>
  <span class="token keyword">module</span> <span class="token punctuation">{</span>
    <span class="token comment"># This module should only include reference to the Vault provider. Terraform</span>
    <span class="token comment"># will automatically work out which providers to supply based on the module</span>
    <span class="token comment"># configuration. The tests will error if a run block requires access to a</span>
    <span class="token comment"># provider that references outputs from a run block that has not executed.</span>
    <span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">&quot;./testing/vault-setup&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;use_aws_provider&quot;</span> <span class="token punctuation">{</span>
  <span class="token comment"># This run block can then use both the `aws` and `vault` providers, as the</span>
  <span class="token comment"># previous run block provided all the data required for the `aws` provider.</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="module-&#x5757;"><a name="module-&#x5757;" class="anchor-navigation-ex-anchor" href="#module-&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.6. module &#x5757;</h2>
<p>&#x60A8;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x7279;&#x5B9A;&#x7684; <code>run</code> &#x5757;&#x6267;&#x884C;&#x7684;&#x6A21;&#x5757;&#x3002;</p>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Terraform &#x9488;&#x5BF9;&#x6B63;&#x5728;&#x6D4B;&#x8BD5;&#x7684;&#x914D;&#x7F6E;&#x4EE3;&#x7801;&#xFF0C;&#x4F9D;&#x6B21;&#x6267;&#x884C;&#x6240;&#x6709; <code>run</code> &#x5757;&#x4E2D;&#x8BBE;&#x5B9A;&#x7684;&#x547D;&#x4EE4;&#x3002;Terraform &#x5728;&#x60A8;&#x6267;&#x884C; <code>terraform test</code> &#x547D;&#x4EE4;&#x7684;&#x76EE;&#x5F55;&#xFF08;&#x6216;&#x8005;&#x60A8;&#x7528; <code>-chdir</code> &#x53C2;&#x6570;&#x6307;&#x5411;&#x7684;&#x76EE;&#x5F55;&#xFF09;&#x5185;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;&#x3002;&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x4E5F;&#x5141;&#x8BB8;&#x7528;&#x6237;&#x4F7F;&#x7528; <code>module</code> &#x5757;&#x66F4;&#x6539;&#x76EE;&#x6807;&#x914D;&#x7F6E;&#x3002;</p>
<p>&#x4E0E;&#x4F20;&#x7EDF;&#x7684; <a href="4.2.&#x4F7F;&#x7528;&#x6A21;&#x5757;.html"><code>module</code> &#x5757;</a>&#x4E0D;&#x540C;&#xFF0C;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x7684; <code>module</code> &#x5757; <em>&#x4EC5;</em> &#x652F;&#x6301; <a href="4.2.&#x4F7F;&#x7528;&#x6A21;&#x5757;.html#&#x6A21;&#x5757;&#x6E90;"><code>e</code></a> &#x5C5E;&#x6027;&#x548C; <a href="4.2.&#x4F7F;&#x7528;&#x6A21;&#x5757;.html#&#x6A21;&#x5757;&#x7248;&#x672C;&#x7EA6;&#x675F;"><code>version</code></a> &#x5C5E;&#x6027;&#x3002;&#x901A;&#x5E38;&#x901A;&#x8FC7;&#x4F20;&#x7EDF;&#x7684; <code>module</code> &#x5757;&#x63D0;&#x4F9B;&#x7684;&#x5176;&#x4F59;&#x5C5E;&#x6027;&#x5E94;&#x7531; <code>run</code> &#x5757;&#x5185;&#x7684;&#x66FF;&#x4EE3;&#x5C5E;&#x6027;&#x548C;&#x5757;&#x63D0;&#x4F9B;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong> Terraform &#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x53EA;&#x652F;&#x6301; <code>e</code> &#x5C5E;&#x6027;&#x4E2D;&#x7684; <a href="4.2.&#x4F7F;&#x7528;&#x6A21;&#x5757;.html#&#x672C;&#x5730;&#x8DEF;&#x5F84;">&#x672C;&#x5730;</a> &#x548C; <a href="4.2.&#x4F7F;&#x7528;&#x6A21;&#x5757;.html#terraform-registry">&#x6CE8;&#x518C;&#x8868;</a> &#x6A21;&#x5757;&#x3002;</p>
<p>&#x5728;&#x6267;&#x884C;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x65F6;&#xFF0C;<code>run</code> &#x5757;&#x5185;&#x7684;&#x6240;&#x6709;&#x5176;&#x4ED6;&#x5757;&#x548C;&#x5C5E;&#x6027;&#x90FD;&#x53D7;&#x652F;&#x6301;&#xFF0C;<code>assert</code> &#x5757;&#x6267;&#x884C;&#x65F6;&#x4F7F;&#x7528;&#x6765;&#x81EA;&#x5176;&#x4ED6;&#x6A21;&#x5757;&#x7684;&#x503C;&#x3002;&#x8FD9;&#x5728; <a href="#&#x6A21;&#x5757;&#x72B6;&#x6001;">&#x6A21;&#x5757;&#x72B6;&#x6001;</a> &#x4E2D;&#x6709;&#x66F4;&#x8BE6;&#x7EC6;&#x7684;&#x8BF4;&#x660E;&#x3002;</p>
<p>&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D; <code>modules</code> &#x5757;&#x7684;&#x4E24;&#x4E2A;&#x793A;&#x4F8B;&#x7528;&#x4F8B;&#x662F;&#xFF1A;</p>
<ol>
<li>&#x4E00;&#x4E2A;&#x8BBE;&#x7F6E;&#x6A21;&#x5757;&#xFF0C;&#x4E3A;&#x5F85;&#x6D4B; Terraform &#x914D;&#x7F6E;&#x4EE3;&#x7801;&#x521B;&#x5EFA;&#x6D4B;&#x8BD5;&#x6240;&#x9700;&#x7684;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#x3002;</li>
<li>&#x4E00;&#x4E2A;&#x52A0;&#x8F7D;&#x6A21;&#x5757;&#xFF0C;&#x7528;&#x4E8E;&#x52A0;&#x8F7D;&#x548C;&#x9A8C;&#x8BC1; Terraform &#x914D;&#x7F6E;&#x4EE3;&#x7801;&#x672A;&#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x7684;&#x6B21;&#x8981;&#x57FA;&#x7840;&#x8BBE;&#x65BD;&#xFF08;&#x5982;&#x6570;&#x636E;&#x6E90;&#xFF09;&#x3002;</li>
</ol>
<p>&#x4EE5;&#x4E0B;&#x793A;&#x4F8B;&#x6F14;&#x793A;&#x4E86;&#x8FD9;&#x4E24;&#x79CD;&#x7528;&#x4F8B;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x6709;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#xFF0C;&#x5B83;&#x5C06;&#x521B;&#x5EFA;&#x5E76;&#x5C06;&#x591A;&#x4E2A;&#x6587;&#x4EF6;&#x52A0;&#x8F7D;&#x5230;&#x5DF2;&#x521B;&#x5EFA;&#x7684; S3 &#x5B58;&#x50A8;&#x6876;&#x4E2D;&#x3002;&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x8981;&#x6D4B;&#x8BD5;&#x7684;&#x914D;&#x7F6E;&#x3002;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># main.tf</span>

<span class="token keyword">variable<span class="token type variable"> &quot;bucket&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">type</span> <span class="token punctuation">=</span> string
<span class="token punctuation">}</span>

<span class="token keyword">variable<span class="token type variable"> &quot;files&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">type</span> <span class="token punctuation">=</span> map(string)
<span class="token punctuation">}</span>

<span class="token keyword">data <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> var.bucket
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_object&quot;</span></span> <span class="token string">&quot;object&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">for_each</span> <span class="token punctuation">=</span> var.files

  <span class="token property">bucket</span> <span class="token punctuation">=</span> data.aws_s3_bucket.bucket.id
  <span class="token property">key</span> <span class="token punctuation">=</span> each.key
  <span class="token property">source</span> <span class="token punctuation">=</span> each.value

  <span class="token property">etag</span> <span class="token punctuation">=</span> filemd5(each.value)
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x914D;&#x7F6E;&#x6A21;&#x5757;&#x521B;&#x5EFA;&#x8FD9;&#x4E2A; S3 &#x5B58;&#x50A8;&#x6876;&#xFF0C;&#x8FD9;&#x6837;&#x5728;&#x6D4B;&#x8BD5;&#x65F6;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># testing/setup/main.tf</span>

<span class="token keyword">variable<span class="token type variable"> &quot;bucket&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">type</span> <span class="token punctuation">=</span> string
<span class="token punctuation">}</span>

<span class="token keyword">resource <span class="token type variable">&quot;aws_s3_bucket&quot;</span></span> <span class="token string">&quot;bucket&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> var.bucket
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7B2C;&#x4E09;&#x6B65;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x52A0;&#x8F7D;&#x6A21;&#x5757;&#xFF0C;&#x8BFB;&#x53D6; S3 &#x5B58;&#x50A8;&#x6876;&#x4E2D;&#x7684;&#x6587;&#x4EF6;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x6BD4;&#x8F83;&#x7275;&#x5F3A;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5B8C;&#x5168;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5728;&#x521B;&#x5EFA;&#x8FD9;&#x4E9B;&#x6587;&#x4EF6;&#x7684;&#x6A21;&#x5757;&#x4E2D;&#x521B;&#x5EFA;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x6E90;&#xFF0C;&#x4F46;&#x5B83;&#x5728;&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x5F88;&#x597D;&#x5730;&#x6F14;&#x793A;&#x5982;&#x4F55;&#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># testing/loader/main.tf</span>

<span class="token keyword">variable<span class="token type variable"> &quot;bucket&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">type</span> <span class="token punctuation">=</span> string
<span class="token punctuation">}</span>

<span class="token keyword">data <span class="token type variable">&quot;aws_s3_objects&quot;</span></span> <span class="token string">&quot;objects&quot;</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> var.bucket
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x628A;&#x521A;&#x624D;&#x521B;&#x5EFA;&#x7684;&#x591A;&#x4E2A;&#x52A9;&#x624B;&#x6A21;&#x5757;&#x4EE5;&#x53CA;&#x5F85;&#x6D4B;&#x6A21;&#x5757;&#x7F16;&#x7EC7;&#x5728;&#x4E00;&#x8D77;&#x5F62;&#x6210;&#x4E00;&#x4E2A;&#x6709;&#x6548;&#x7684;&#x6D4B;&#x8BD5;&#x914D;&#x7F6E;&#xFF1A;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># file_count.tftest.hcl</span>

<span class="token keyword">variables</span> <span class="token punctuation">{</span>
  <span class="token property">bucket</span> <span class="token punctuation">=</span> <span class="token string">&quot;my_test_bucket&quot;</span>
  <span class="token property">files</span> <span class="token punctuation">=</span> <span class="token punctuation">{</span>
    <span class="token property">&quot;file-one.txt&quot;</span>: <span class="token string">&quot;data/files/file_one.txt&quot;</span>
    <span class="token property">&quot;file-two.txt&quot;</span>: <span class="token string">&quot;data/files/file_two.txt&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">provider<span class="token type variable"> &quot;aws&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">region</span> <span class="token punctuation">=</span> <span class="token string">&quot;us-east-1&quot;</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;setup&quot;</span> <span class="token punctuation">{</span>
  <span class="token comment"># Create the S3 bucket we will use later.</span>

  <span class="token keyword">module</span> <span class="token punctuation">{</span>
    <span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">&quot;./testing/setup&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;execute&quot;</span> <span class="token punctuation">{</span>
  <span class="token comment"># This is empty, we just run the configuration under test using all the default settings.</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;verify&quot;</span> <span class="token punctuation">{</span>
  <span class="token comment"># Load and count the objects created in the &quot;execute&quot; run block.</span>

  <span class="token keyword">module</span> <span class="token punctuation">{</span>
    <span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">&quot;./testing/loader&quot;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span> <span class="token punctuation">=</span> length(data.aws_s3_objects.objects.keys) <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token number">2</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;created the wrong number of s3 objects&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x6A21;&#x5757;&#x72B6;&#x6001;"><a name="&#x6A21;&#x5757;&#x72B6;&#x6001;" class="anchor-navigation-ex-anchor" href="#&#x6A21;&#x5757;&#x72B6;&#x6001;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.6.1. &#x6A21;&#x5757;&#x72B6;&#x6001;</h3>
<p>&#x5F53; Terraform &#x6267;&#x884C; <code>terraform test</code> &#x547D;&#x4EE4;&#x65F6;&#xFF0C;Terraform &#x4F1A;&#x4E3A;&#x6BCF;&#x4E2A;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x7EF4;&#x62A4;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x603B;&#x662F;&#x81F3;&#x5C11;&#x6709;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x7EF4;&#x62A4;&#x5728;&#x6D4B;&#x8BD5;&#x4E0B;&#x7684; Terraform &#x914D;&#x7F6E;&#x4EE3;&#x7801;&#x7684;&#x72B6;&#x6001;&#x3002;&#x8FD9;&#x4E2A;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x7531;&#x6240;&#x6709;&#x6CA1;&#x6709; <code>module</code> &#x5757;&#x6307;&#x5B9A;&#x8981;&#x52A0;&#x8F7D;&#x7684;&#x66FF;&#x4EE3;&#x6A21;&#x5757;&#x7684; <code>run</code> &#x5757;&#x5171;&#x4EAB;&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;Terraform &#x52A0;&#x8F7D;&#x7684;&#x6BCF;&#x4E2A;&#x66FF;&#x4EE3;&#x6A21;&#x5757;&#x90FD;&#x6709;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x3002;&#x4E00;&#x4E2A;&#x66FF;&#x4EE3;&#x6A21;&#x5757;&#x7684;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x88AB;&#x6267;&#x884C;&#x7ED9;&#x5B9A;&#x6A21;&#x5757;&#x7684;&#x6240;&#x6709; <code>run</code> &#x5757;&#x5171;&#x4EAB;&#x3002;</p>
<p>Terraform &#x56E2;&#x961F;&#x5BF9;&#x4EFB;&#x4F55;&#x9700;&#x8981;&#x624B;&#x52A8;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x6216;&#x5728; <code>test</code> &#x547D;&#x4EE4;&#x4E2D;&#x5BF9;&#x540C;&#x4E00;&#x72B6;&#x6001;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x914D;&#x7F6E;&#x7684;&#x7528;&#x4F8B;&#x611F;&#x5174;&#x8DA3;&#x3002;&#x5982;&#x679C;&#x4F60;&#x6709;&#x4E00;&#x4E2A;&#x7528;&#x4F8B;&#xFF0C;&#x8BF7;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A; <a href="https://github.com/hashicorp/terraform/issues/new/choose" target="_blank">issue</a>&#x5E76;&#x4E0E;&#x6211;&#x4EEC;&#x5206;&#x4EAB;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x793A;&#x4F8B;&#x4F7F;&#x7528;&#x6CE8;&#x91CA;&#x6765;&#x89E3;&#x91CA;&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x7684;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x7684;&#x6765;&#x6E90;&#x3002;&#x5728;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;Terraform &#x521B;&#x5EFA;&#x5E76;&#x7BA1;&#x7406;&#x4E86;&#x603B;&#x5171;&#x4E09;&#x4E2A;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x662F;&#x9488;&#x5BF9;&#x6D4B;&#x8BD5;&#x4E0B;&#x7684;&#x4E3B;&#x6A21;&#x5757;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x662F;&#x9488;&#x5BF9;&#x8BBE;&#x7F6E;&#x6A21;&#x5757;&#xFF0C;&#x7B2C;&#x4E09;&#x4E2A;&#x662F;&#x9488;&#x5BF9;&#x52A0;&#x8F7D;&#x6A21;&#x5757;&#x3002;</p>
<pre class="language-"><code class="lang-hcl">run <span class="token string">&quot;setup&quot;</span> <span class="token punctuation">{</span>

  <span class="token comment"># This run block references an alternate module and is the first run block</span>
  <span class="token comment"># to reference this particular alternate module. Therefore, Terraform creates</span>
  <span class="token comment"># and populates a new empty state file for this run block.</span>

  <span class="token keyword">module</span> <span class="token punctuation">{</span>
    <span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">&quot;./testing/setup&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;init&quot;</span> <span class="token punctuation">{</span>

  <span class="token comment"># This run block does not reference an alternate module, so it uses the main</span>
  <span class="token comment"># state file for the configuration under test. As this is the first run block</span>
  <span class="token comment"># to reference the main configuration, the previously empty state file now</span>
  <span class="token comment"># contains the resources created by this run block.</span>

  <span class="token keyword">assert</span> <span class="token punctuation">{</span>
    <span class="token comment"># In practice we&apos;d do some interesting checks and tests here but the</span>
    <span class="token comment"># assertions aren&apos;t important for this example.</span>
  <span class="token punctuation">}</span>

  <span class="token comment"># ... more assertions ...</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;update_setup&quot;</span> <span class="token punctuation">{</span>

  <span class="token comment"># We&apos;ve now re-referenced the setup module, so the state file that was created</span>
  <span class="token comment"># for the first &quot;setup&quot; run block will be reused. It will contain any</span>
  <span class="token comment"># resources that were created as part of the other run block before this run</span>
  <span class="token comment"># block executes and will be updated with any changes made by this run block</span>
  <span class="token comment"># after.</span>

  <span class="token keyword">module</span> <span class="token punctuation">{</span>
    <span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">&quot;./testing/setup&quot;</span>
  <span class="token punctuation">}</span>

  <span class="token keyword">variables</span> <span class="token punctuation">{</span>
    <span class="token comment"># In practice, we&apos;d likely make some changes to the module compared to the</span>
    <span class="token comment"># first run block here. Otherwise, there would be no point recalling the</span>
    <span class="token comment"># module.</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;update&quot;</span> <span class="token punctuation">{</span>

  <span class="token comment"># As with the &quot;init&quot; run block, we are executing against the main configuration</span>
  <span class="token comment"># again. This means we&apos;d load the main state file that was initially populated</span>
  <span class="token comment"># by the &quot;init&quot; run block, and any changes made by this &quot;run&quot; block will be</span>
  <span class="token comment"># carried forward to any future run blocks that execute against the main</span>
  <span class="token comment"># configuration.</span>

  <span class="token comment"># ... updated variables ...</span>

  <span class="token comment"># ... assertions ...</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;loader&quot;</span> <span class="token punctuation">{</span>

  <span class="token comment"># This run block is now referencing our second alternate module so will create</span>
  <span class="token comment"># our third and final state file. The other two state files are managing</span>
  <span class="token comment"># resources from the main configuration and resources from the setup module.</span>
  <span class="token comment"># We are getting a new state file for this run block as the loader module has</span>
  <span class="token comment"># not previously been referenced by any run blocks.</span>

  <span class="token keyword">module</span> <span class="token punctuation">{</span>
    <span class="token property">source</span> <span class="token punctuation">=</span> <span class="token string">&quot;./testing/loader&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h4 id="&#x6A21;&#x5757;&#x7684;&#x6E05;&#x7406;"><a name="&#x6A21;&#x5757;&#x7684;&#x6E05;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x6A21;&#x5757;&#x7684;&#x6E05;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6A21;&#x5757;&#x7684;&#x6E05;&#x7406;</h4>
<p>&#x5728;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x6267;&#x884C;&#x7ED3;&#x675F;&#x65F6;&#xFF0C;Terraform &#x4F1A;&#x8BD5;&#x56FE;&#x9500;&#x6BC1;&#x5728;&#x8BE5;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x521B;&#x5EFA;&#x7684;&#x6BCF;&#x4E2A;&#x8D44;&#x6E90;&#x3002;&#x5F53; Terraform &#x52A0;&#x8F7D;&#x66FF;&#x4EE3;&#x6A21;&#x5757;&#x65F6;&#xFF0C;Terraform &#x9500;&#x6BC1;&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#x7684;&#x987A;&#x5E8F;&#x5F88;&#x91CD;&#x8981;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x7B2C;&#x4E00;&#x4E2A; <a href="#module-&#x5757;">&#x6A21;&#x5757;</a> &#x793A;&#x4F8B;&#x4E2D;&#xFF0C;Terraform &#x4E0D;&#x80FD;&#x5728; &quot;execute&quot; <code>run</code> &#x5757;&#x4E2D;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x4E4B;&#x524D;&#x9500;&#x6BC1;&#x5728; &quot;setup&quot; <code>run</code> &#x5757;&#x4E2D;&#x521B;&#x5EFA;&#x7684;&#x8D44;&#x6E90;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x5728; &quot;setup&quot; &#x6B65;&#x9AA4;&#x4E2D;&#x521B;&#x5EFA;&#x7684; S3 &#x6876;&#x5728;&#x5305;&#x542B;&#x5BF9;&#x8C61;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x65E0;&#x6CD5;&#x88AB;&#x9500;&#x6BC1;&#x3002;</p>
<p>Terraform &#x6309;&#x7167; <code>run</code> &#x5757;&#x7684;&#x53CD;&#x5411;&#x987A;&#x5E8F;&#x9500;&#x6BC1;&#x8D44;&#x6E90;&#x3002;&#x5728;&#x6700;&#x8FD1;&#x7684; <a href="#&#x6A21;&#x5757;&#x72B6;&#x6001;">&#x4F8B;&#x5B50;</a> &#x4E2D;&#xFF0C;&#x6709;&#x4E09;&#x4E2A;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x3002;&#x4E00;&#x4E2A;&#x7528;&#x4E8E;&#x4E3B;&#x72B6;&#x6001;&#xFF0C;&#x4E00;&#x4E2A;&#x7528;&#x4E8E; <code>./testing/loader</code> &#x6A21;&#x5757;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x7528;&#x4E8E; <code>./testing/setup</code> &#x6A21;&#x5757;&#x3002;&#x7531;&#x4E8E; <code>./testing/loader</code> &#x72B6;&#x6001;&#x6587;&#x4EF6;&#x6700;&#x8FD1;&#x88AB;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x8FD0;&#x884C;&#x5757;&#x5F15;&#x7528;&#xFF0C;&#x56E0;&#x6B64;&#x9996;&#x5148;&#x88AB;&#x9500;&#x6BC1;&#x3002;&#x4E3B;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x5C06;&#x88AB;&#x7B2C;&#x4E8C;&#x4E2A;&#x9500;&#x6BC1;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x88AB; &quot;update&quot; <code>run</code> &#x5757;&#x5F15;&#x7528;&#x3002;&#x7136;&#x540E; <code>./testing/setup</code> &#x72B6;&#x6001;&#x6587;&#x4EF6;&#x5C06;&#x88AB;&#x6700;&#x540E;&#x9500;&#x6BC1;&#x3002;</p>
<p>&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x524D;&#x4E24;&#x4E2A; <code>run</code> &#x5757; &quot;setup&quot; &#x548C; &quot;init&quot; &#x5728;&#x9500;&#x6BC1;&#x64CD;&#x4F5C;&#x4E2D;&#x4E0D;&#x505A;&#x4EFB;&#x4F55;&#x4E8B;&#x60C5;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x4EEC;&#x7684;&#x72B6;&#x6001;&#x6587;&#x4EF6;&#x88AB;&#x540E;&#x7EED;&#x7684; run &#x5757;&#x4F7F;&#x7528;&#xFF0C;&#x5E76;&#x4E14;&#x5DF2;&#x7ECF;&#x88AB;&#x9500;&#x6BC1;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x5355;&#x4E2A;&#x8BBE;&#x7F6E;&#x6A21;&#x5757;&#x4F5C;&#x4E3A;&#x66FF;&#x4EE3;&#x6A21;&#x5757;&#xFF0C;&#x5E76;&#x4E14;&#x5B83;&#x9996;&#x5148;&#x6267;&#x884C;&#xFF0C;&#x6216;&#x8005;&#x4F60;&#x4E0D;&#x4F7F;&#x7528;&#x4EFB;&#x4F55;&#x66FF;&#x4EE3;&#x6A21;&#x5757;&#xFF0C;&#x90A3;&#x4E48;&#x9500;&#x6BC1;&#x987A;&#x5E8F;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x4F60;&#x3002;&#x66F4;&#x590D;&#x6742;&#x7684;&#x60C5;&#x51B5;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x4ED4;&#x7EC6;&#x8003;&#x8651;&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x8D44;&#x6E90;&#x7684;&#x9500;&#x6BC1;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x5B8C;&#x6210;&#x3002;</p>
<h2 id="&#x9884;&#x671F;&#x5931;&#x8D25;"><a name="&#x9884;&#x671F;&#x5931;&#x8D25;" class="anchor-navigation-ex-anchor" href="#&#x9884;&#x671F;&#x5931;&#x8D25;"><i class="fa fa-link" aria-hidden="true"></i></a>1.7.1.7. &#x9884;&#x671F;&#x5931;&#x8D25;</h2>
<p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x6267;&#x884C; Terraform &#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x671F;&#x95F4;&#xFF0C;&#x4EFB;&#x4F55;<a href="https://developer.hashicorp.com/terraform/language/expressions/custom-conditions" target="_blank">&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;</a>&#xFF0C;&#x5305;&#x62EC; <code>check</code> &#x5757;&#x65AD;&#x8A00;&#x5931;&#x8D25;&#xFF0C;&#x5219;&#x6574;&#x4F53;&#x547D;&#x4EE4;&#x4F1A;&#x5C06;&#x6D4B;&#x8BD5;&#x62A5;&#x544A;&#x4E3A;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x7136;&#x800C;&#xFF0C;&#x6211;&#x4EEC;&#x7ECF;&#x5E38;&#x60F3;&#x8981;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x8FD0;&#x884C;&#x5931;&#x8D25;&#x65F6;&#x7684;&#x884C;&#x4E3A;&#x3002;Terraform &#x4E3A;&#x6B64;&#x7528;&#x4F8B;&#x63D0;&#x4F9B;&#x4E86; <code>expect_failures</code> &#x5C5E;&#x6027;&#x3002;</p>
<p>&#x5728;&#x6BCF;&#x4E2A; <code>run</code> &#x5757;&#x4E2D;&#xFF0C;<code>expect_failures</code> &#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x5E94;&#x8BE5;&#x5BFC;&#x81F4;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x68C0;&#x67E5;&#x5931;&#x8D25;&#x7684;&#x53EF;&#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#xFF08;&#x8D44;&#x6E90;&#xFF0C;&#x6570;&#x636E;&#x6E90;&#xFF0C;&#x68C0;&#x67E5;&#x5757;&#xFF0C;&#x8F93;&#x5165;&#x53D8;&#x91CF;&#x548C;&#x8F93;&#x51FA;&#xFF09;&#x7684;&#x5217;&#x8868;&#x3002;&#x5982;&#x679C;&#x60A8;&#x6307;&#x5B9A;&#x7684;&#x53EF;&#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#x62A5;&#x544A;&#x95EE;&#x9898;&#xFF0C;&#x6D4B;&#x5219;&#x8BD5;&#x901A;&#x8FC7;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x62A5;&#x544A;&#x9519;&#x8BEF;&#xFF0C;&#x90A3;&#x4E48;&#x6D4B;&#x8BD5;&#x603B;&#x4F53;&#x4E0A;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x60A8;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x5728; <code>expect_failures</code> &#x5757;&#x9644;&#x8FD1;&#x7F16;&#x5199;&#x65AD;&#x8A00;&#xFF0C;&#x4F46;&#x60A8;&#x5E94;&#x8BE5;&#x6CE8;&#x610F;&#xFF0C;&#x9664;&#x4E86; <code>check</code> &#x5757;&#x65AD;&#x8A00;&#x5916;&#xFF0C;&#x6240;&#x6709;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x90FD;&#x4F1A;&#x505C;&#x6B62; Terraform &#x7684;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x5728;&#x6D4B;&#x8BD5;&#x6267;&#x884C;&#x671F;&#x95F4;&#x4ECD;&#x7136;&#x9002;&#x7528;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x4E9B;&#x65AD;&#x8A00;&#x5E94;&#x8BE5;&#x53EA;&#x8003;&#x8651;&#x4F60;&#x786E;&#x5B9A;&#x4F1A;&#x5728;&#x53EF;&#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#x5E94;&#x8BE5;&#x5931;&#x8D25;&#x4E4B;&#x524D;&#x53EF;&#x77E5;&#x7684;&#x503C;&#x3002;&#x60A8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5F15;&#x7528;&#x6216;&#x5728;&#x4E3B;&#x914D;&#x7F6E;&#x4E2D;&#x7684; <code>depends_on</code> &#x5143;&#x53C2;&#x6570;&#x6765;&#x7BA1;&#x7406;&#x8FD9;&#x4E00;&#x70B9;&#x3002;</p>
<p>&#x8FD9;&#x4E5F;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x9664;&#x4E86; <code>check</code> &#x5757;&#xFF0C;&#x4F60;&#x53EA;&#x80FD;&#x53EF;&#x9760;&#x5730;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x53EF;&#x68C0;&#x67E5;&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x6211;&#x4EEC;&#x652F;&#x6301;&#x5728; <code>expect_failures</code> &#x5C5E;&#x6027;&#x4E2D;&#x5217;&#x51FA;&#x53EF;&#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#x7684;&#x5217;&#x8868;&#xFF0C;&#x4EC5;&#x7528;&#x4E8E; <code>check</code> &#x5757;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4E00;&#x4E2A;&#x5FEB;&#x901F;&#x793A;&#x4F8B;&#x6F14;&#x793A;&#x4E86;&#x6D4B;&#x8BD5;&#x8F93;&#x5165;&#x53D8;&#x91CF;&#x7684; <code>validation</code> &#x5757;&#x3002;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x5FC5;&#x987B;&#x662F;&#x5076;&#x6570;&#x7684;&#x5355;&#x4E00;&#x8F93;&#x5165;&#x53D8;&#x91CF;&#x3002;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># main.tf</span>

<span class="token keyword">variable<span class="token type variable"> &quot;input&quot; </span></span><span class="token punctuation">{</span>
  <span class="token property">type</span> <span class="token punctuation">=</span> number

  <span class="token keyword">validation</span> <span class="token punctuation">{</span>
    <span class="token property">condition</span> <span class="token punctuation">=</span> var.input % <span class="token number">2</span> <span class="token punctuation">=</span><span class="token punctuation">=</span> <span class="token number">0</span>
    <span class="token property">error_message</span> <span class="token punctuation">=</span> <span class="token string">&quot;must be even number&quot;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x5305;&#x542B;&#x4E86;&#x4E24;&#x4E2A; <code>run</code> &#x5757;&#x3002;&#x4E00;&#x4E2A;&#x9A8C;&#x8BC1;&#x4E86;&#x6211;&#x4EEC;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x5728;&#x5076;&#x6570;&#x6761;&#x4EF6;&#x4E0B;&#x662F;&#x901A;&#x8FC7;&#x7684;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x9A8C;&#x8BC1;&#x8F93;&#x5165;&#x5947;&#x6570;&#x65F6;&#x4F1A;&#x5931;&#x8D25;&#x3002;</p>
<pre class="language-"><code class="lang-hcl"><span class="token comment"># input_validation.tftest.hcl</span>

<span class="token keyword">variables</span> <span class="token punctuation">{</span>
  <span class="token property">input</span> <span class="token punctuation">=</span> <span class="token number">0</span>
<span class="token punctuation">}</span>

run <span class="token string">&quot;zero&quot;</span> <span class="token punctuation">{</span>
  <span class="token comment"># The variable defined above is even, so we expect the validation to pass.</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan
<span class="token punctuation">}</span>

run <span class="token string">&quot;one&quot;</span> <span class="token punctuation">{</span>
  <span class="token comment"># This time we set the variable is odd, so we expect the validation to fail.</span>

  <span class="token property">command</span> <span class="token punctuation">=</span> plan

  <span class="token keyword">variables</span> <span class="token punctuation">{</span>
    <span class="token property">input</span> <span class="token punctuation">=</span> <span class="token number">1</span>
  <span class="token punctuation">}</span>

  <span class="token property">expect_failures</span> <span class="token punctuation">=</span> <span class="token punctuation">[</span>
    var.input,
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre>
<p><strong>&#x6CE8;&#x610F;</strong>&#xFF1A;Terraform &#x53EA;&#x671F;&#x671B;&#x5728; <code>run</code> &#x5757;&#x7684; <code>command</code> &#x5C5E;&#x6027;&#x6307;&#x5B9A;&#x7684;&#x64CD;&#x4F5C;&#x4E2D;&#x51FA;&#x73B0;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x5728;&#x4F7F;&#x7528; <code>command = apply</code> &#x7684; <code>run</code> &#x5757;&#x4E2D;&#x4F7F;&#x7528; <code>expect_failures</code> &#x65F6;&#x8981;&#x5C0F;&#x5FC3;&#x3002;&#x4E00;&#x4E2A; <code>run</code> &#x5757;&#x4E2D;&#x7684; <code>command = apply</code> &#x5982;&#x679C;&#x671F;&#x671B;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x5931;&#x8D25;&#xFF0C;&#x90A3;&#x4E48;&#x5982;&#x679C;&#x8BE5;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x5728; <code>plan</code> &#x671F;&#x95F4;&#x5931;&#x8D25;&#xFF0C;&#x6574;&#x4F53;&#x5C06;&#x4F1A;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x8FD9;&#x5728;&#x903B;&#x8F91;&#x4E0A;&#x662F;&#x6B63;&#x786E;&#x7684;&#xFF0C;&#x56E0;&#x4E3A; <code>run</code> &#x5757;&#x671F;&#x671B;&#x80FD;&#x591F;&#x8FD0;&#x884C;&#x5E94;&#x7528;&#x64CD;&#x4F5C;&#xFF0C;&#x4F46;&#x7531;&#x4E8E; <code>plan</code> &#x5931;&#x8D25;&#x800C;&#x4E0D;&#x80FD;&#x8FD0;&#x884C;&#xFF0C;&#x4F46;&#x8FD9;&#x4E5F;&#x53EF;&#x80FD;&#x4F1A;&#x5F15;&#x8D77;&#x6DF7;&#x6DC6;&#xFF0C;&#x56E0;&#x4E3A;&#x5373;&#x4F7F;&#x90A3;&#x4E2A;&#x5931;&#x8D25;&#x88AB;&#x6807;&#x8BB0;&#x4E3A;&#x9884;&#x671F;&#x7684;&#xFF0C;&#x4F60;&#x8FD8;&#x662F;&#x4F1A;&#x5728;&#x8BCA;&#x65AD;&#x4E2D;&#x770B;&#x5230;&#x5931;&#x8D25;&#x3002;</p>
<p>&#x6709;&#x65F6;&#xFF0C;Terraform &#x5728;&#x8BA1;&#x5212;&#x9636;&#x6BB5;&#x4E0D;&#x6267;&#x884C;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#xFF0C;&#x56E0;&#x4E3A;&#x8BE5;&#x6761;&#x4EF6;&#x4F9D;&#x8D56;&#x4E8E;&#x53EA;&#x6709;&#x5728; Terraform &#x521B;&#x5EFA;&#x5F15;&#x7528;&#x8D44;&#x6E90;&#x540E;&#x624D;&#x53EF;&#x7528;&#x7684;&#x8BA1;&#x7B97;&#x5C5E;&#x6027;&#x3002;&#x5728;&#x8FD9;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x8BBE;&#x7F6E; <code>command = apply</code> &#x65F6;&#x4F7F;&#x7528; <code>expect_failures</code> &#x5757;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x5EFA;&#x8BAE;&#x53EA;&#x5728; <code>command = plan</code> &#x65F6;&#x4F7F;&#x7528; <code>expect_failures</code>&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;</strong>&#xFF1A;&#x9884;&#x671F;&#x7684;&#x5931;&#x8D25;&#x53EA;&#x9002;&#x7528;&#x4E8E;&#x7528;&#x6237;&#x5B9A;&#x4E49;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x3002;</p>
<p>&#x9664;&#x4E86;&#x5728;&#x53EF;&#x68C0;&#x67E5;&#x5BF9;&#x8C61;&#x4E2D;&#x6307;&#x5B9A;&#x7684;&#x9884;&#x671F;&#x5931;&#x8D25;&#x4E4B;&#x5916;&#x7684;&#x5176;&#x4ED6;&#x79CD;&#x7C7B;&#x7684;&#x5931;&#x8D25;&#x4ECD;&#x4F1A;&#x5BFC;&#x81F4;&#x6574;&#x4F53;&#x6D4B;&#x8BD5;&#x5931;&#x8D25;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x4E00;&#x4E2A;&#x671F;&#x671B;&#x5E03;&#x5C14;&#x503C;&#x4F5C;&#x4E3A;&#x8F93;&#x5165;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x5982;&#x679C; Terraform &#x6536;&#x5230;&#x7684;&#x662F;&#x9519;&#x8BEF;&#x7684;&#x503C;&#x7C7B;&#x578B;&#xFF0C;&#x5373;&#x4F7F;&#x8BE5;&#x53D8;&#x91CF;&#x5305;&#x542B;&#x5728; <code>expect_failures</code> &#x5C5E;&#x6027;&#x4E2D;&#xFF0C;&#x4E5F;&#x4F1A;&#x5BFC;&#x81F4;&#x5468;&#x56F4;&#x7684;&#x6D4B;&#x8BD5;&#x5931;&#x8D25;&#x3002;</p>
<p><code>expect_failures</code> &#x5C5E;&#x6027;&#x5305;&#x542B;&#x5728;&#x5176;&#x4E2D;&#x662F;&#x4E3A;&#x4E86;&#x5141;&#x8BB8;&#x4F5C;&#x8005;&#x6D4B;&#x8BD5;&#x4ED6;&#x4EEC;&#x7684;&#x914D;&#x7F6E;&#x548C;&#x4EFB;&#x4F55;&#x5B9A;&#x4E49;&#x7684;&#x903B;&#x8F91;&#x3002;&#x50CF;&#x524D;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#x7684;&#x7C7B;&#x578B;&#x4E0D;&#x5339;&#x914D;&#x9519;&#x8BEF;&#xFF0C;&#x4E0D;&#x662F; Terraform &#x4F5C;&#x8005;&#x5E94;&#x8BE5;&#x62C5;&#x5FC3;&#x548C;&#x6D4B;&#x8BD5;&#x7684;&#x4E8B;&#x60C5;&#xFF0C;&#x56E0;&#x4E3A; Terraform &#x672C;&#x8EAB;&#x4F1A;&#x5904;&#x7406;&#x5F3A;&#x5236;&#x7C7B;&#x578B;&#x7EA6;&#x675F;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x4F60;&#x53EA;&#x80FD;&#x5728;&#x81EA;&#x5B9A;&#x4E49;&#x6761;&#x4EF6;&#x4E2D; <code>expect_failures</code>&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="5.22.5.show.html" class="navigation navigation-prev " aria-label="Previous page: show">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="7.Aha——会心一击.html" class="navigation navigation-next " aria-label="Next page: Aha——会心一击">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Terraform Test","level":"1.7","depth":1,"next":{"title":"Aha——会心一击","level":"1.8","depth":1,"path":"7.Aha——会心一击.md","ref":"./7.Aha——会心一击.md","articles":[{"title":"有条件创建","level":"1.8.1","depth":2,"path":"7.1.有条件创建.md","ref":"./7.1.有条件创建.md","articles":[]},{"title":"依赖反转","level":"1.8.2","depth":2,"path":"7.2.依赖反转.md","ref":"./7.2.依赖反转.md","articles":[]},{"title":"多可用区分布","level":"1.8.3","depth":2,"path":"7.3.多可用区分布.md","ref":"./7.3.多可用区分布.md","articles":[]},{"title":"provisioner与user_data","level":"1.8.4","depth":2,"path":"7.4.provisioner与user_data.md","ref":"./7.4.provisioner与user_data.md","articles":[]},{"title":"destroy-provisioner中使用变量","level":"1.8.5","depth":2,"path":"7.5.destroy-provisioner中使用变量.md","ref":"./7.5.destroy-provisioner中使用变量.md","articles":[]},{"title":"利用null_resource的triggers触发其他资源更新","level":"1.8.6","depth":2,"path":"7.6.利用null_resource的triggers触发其他资源更新.md","ref":"./7.6.利用null_resource的triggers触发其他资源更新.md","articles":[]},{"title":"利用 null_resource 搭配 replace_triggered_by 更新无法从服务端读取内容的属性","level":"1.8.7","depth":2,"path":"7.7.利用null_resource搭配replace_triggered_by更新无法读取的属性.md","ref":"./7.7.利用null_resource搭配replace_triggered_by更新无法读取的属性.md","articles":[]},{"title":"创建资源的条件依赖另一个资源的输出时怎么办","level":"1.8.8","depth":2,"path":"7.8.创建资源的条件依赖另一个资源的输出时怎么办.md","ref":"7.8.创建资源的条件依赖另一个资源的输出时怎么办.md","articles":[]},{"title":"利用 create_before_destroy 调整资源 Update 的执行顺序","level":"1.8.9","depth":2,"path":"7.9.利用create_before_destroy调整资源update执行的顺序.md","ref":"7.9.利用create_before_destroy调整资源update执行的顺序.md","articles":[]},{"title":"Terraform 与自动化","level":"1.8.10","depth":2,"path":"7.10.terraform与自动化.md","ref":"7.10.terraform与自动化.md","articles":[]}]},"previous":{"title":"show","level":"1.6.22.5","depth":3,"path":"5.22.5.show.md","ref":"./5.22.5.show.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-highlight","prism","prism-themes","fontsettings","sharing","-lunr","-search","search-pro","livereload","image-captions","sitemap-general","anchor-navigation-ex"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"prism":{"css":["prism-themes/themes/prism-vsc-dark-plus.css"],"lang":{"shell":"bash","cmd":"bash"}},"livereload":{},"search-pro":{},"sitemap-general":{"prefix":"https://lonegunmanb.github.io/introduction-terraform/"},"fontsettings":{"family":"sans","size":2,"theme":"white"},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"prism-themes":{},"sharing":{"all":["facebook","google","twitter","weibo","instapaper"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":true},"image-captions":{"caption":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","variable_name":"_pictures"}},"theme":"default","author":"大可不加冰","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"_pictures":[{"backlink":"index.html#fig1.1.1","level":"1.1","list_caption":"Figure: Nomad、Consul、Vault、Terraform 组成的 HashiStack","alt":"Nomad、Consul、Vault、Terraform 组成的 HashiStack","nro":1,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2020-11-13/1605243540985-image.png","index":1,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"Nomad、Consul、Vault、Terraform 组成的 HashiStack","attributes":{},"skip":false,"key":"1.1.1"},{"backlink":"index.html#fig1.1.2","level":"1.1","list_caption":"Figure: Terraform 著名教材《Terraform Up & Running》，建议直接阅读第二版","alt":"Terraform 著名教材《Terraform Up & Running》，建议直接阅读第二版","nro":2,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2020-12-23/1608689176373-image.png","index":2,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"Terraform 著名教材《Terraform Up & Running》，建议直接阅读第二版","attributes":{},"skip":false,"key":"1.1.2"},{"backlink":"./2.1.Terraform基础概念——Provider.html#fig1.3.1.1","level":"1.3.1","list_caption":"Figure: Terraform通过RPC调用插件，插件代码通过调用SDK操作远程资源","alt":"Terraform通过RPC调用插件，插件代码通过调用SDK操作远程资源","nro":3,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2020-11-16/1605496195438-image.png","index":1,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"Terraform通过RPC调用插件，插件代码通过调用SDK操作远程资源","attributes":{},"skip":false,"key":"1.3.1.1"},{"backlink":"./2.1.Terraform基础概念——Provider.html#fig1.3.1.2","level":"1.3.1","list_caption":"Figure: registry.terraform.io上的插件页面","alt":"registry.terraform.io上的插件页面","nro":4,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2020-11-19/1605799350775-image.png","index":2,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"registry.terraform.io上的插件页面","attributes":{},"skip":false,"key":"1.3.1.2"},{"backlink":"./2.1.Terraform基础概念——Provider.html#fig1.3.1.3","level":"1.3.1","list_caption":"Figure: registry.terraform.io的搜索页面","alt":"registry.terraform.io的搜索页面","nro":5,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2020-11-16/1605498907942-image.png","index":3,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"registry.terraform.io的搜索页面","attributes":{},"skip":false,"key":"1.3.1.3"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.1","level":"1.3.2","list_caption":"Figure: Consul的GUI界面","alt":"Consul的GUI界面","nro":6,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240220224455.png","index":1,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"Consul的GUI界面","attributes":{},"skip":false,"key":"1.3.2.1"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.2","level":"1.3.2","list_caption":"Figure: Consul 中可以看到名为 `localstack-aws` 的键","alt":"Consul 中可以看到名为 `localstack-aws` 的键","nro":7,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240220230042.png","index":2,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"Consul 中可以看到名为 `localstack-aws` 的键","attributes":{},"skip":false,"key":"1.3.2.2"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.3","level":"1.3.2","list_caption":"Figure: 键的内容","alt":"键的内容","nro":8,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240220230136.png","index":3,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"键的内容","attributes":{},"skip":false,"key":"1.3.2.3"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.4","level":"1.3.2","list_caption":"Figure: 键依然存在","alt":"键依然存在","nro":9,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240220230312.png","index":4,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"键依然存在","attributes":{},"skip":false,"key":"1.3.2.4"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.5","level":"1.3.2","list_caption":"Figure: 内容已被清空","alt":"内容已被清空","nro":10,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240220230350.png","index":5,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"内容已被清空","attributes":{},"skip":false,"key":"1.3.2.5"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.6","level":"1.3.2","list_caption":"Figure: 多了一个同名的文件夹","alt":"多了一个同名的文件夹","nro":11,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225100309.png","index":6,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"多了一个同名的文件夹","attributes":{},"skip":false,"key":"1.3.2.6"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.7","level":"1.3.2","list_caption":"Figure: localstack-aws 文件夹内部","alt":"localstack-aws 文件夹内部","nro":12,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225100354.png","index":7,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"localstack-aws 文件夹内部","attributes":{},"skip":false,"key":"1.3.2.7"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.8","level":"1.3.2","list_caption":"Figure: `.lock` 内容","alt":"`.lock` 内容","nro":13,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225180803.png","index":8,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"`.lock` 内容","attributes":{},"skip":false,"key":"1.3.2.8"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.9","level":"1.3.2","list_caption":"Figure: `.lockinfo` 内容","alt":"`.lockinfo` 内容","nro":14,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225180852.png","index":9,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"`.lockinfo` 内容","attributes":{},"skip":false,"key":"1.3.2.9"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.10","level":"1.3.2","list_caption":"Figure: 重新访问Consul","alt":"重新访问Consul","nro":15,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225101100.png","index":10,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"重新访问Consul","attributes":{},"skip":false,"key":"1.3.2.10"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.11","level":"1.3.2","list_caption":"Figure: 将代码复制到不同的文件夹中保存","alt":"将代码复制到不同的文件夹中保存","nro":16,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2020-11-17/1605571444613-image.png","index":11,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"将代码复制到不同的文件夹中保存","attributes":{},"skip":false,"key":"1.3.2.11"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.12","level":"1.3.2","list_caption":"Figure: 重新访问 Consul，目前有一个键","alt":"重新访问 Consul，目前有一个键","nro":17,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225174932.png","index":12,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"重新访问 Consul，目前有一个键","attributes":{},"skip":false,"key":"1.3.2.12"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.13","level":"1.3.2","list_caption":"Figure: Consul 中多了一个 `localstack-aws-env:feature1`","alt":"Consul 中多了一个 `localstack-aws-env:feature1`","nro":18,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225175538.png","index":13,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"Consul 中多了一个 `localstack-aws-env:feature1`","attributes":{},"skip":false,"key":"1.3.2.13"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.14","level":"1.3.2","list_caption":"Figure: 此时 `localstack-aws-env:feature1` 的内容","alt":"此时 `localstack-aws-env:feature1` 的内容","nro":19,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225175806.png","index":14,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"此时 `localstack-aws-env:feature1` 的内容","attributes":{},"skip":false,"key":"1.3.2.14"},{"backlink":"./2.2.Terraform基础概念——状态管理.html#fig1.3.2.15","level":"1.3.2","list_caption":"Figure: `localstack-aws-env:feature1` 被删除了","alt":"`localstack-aws-env:feature1` 被删除了","nro":20,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/20240225180126.png","index":15,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"`localstack-aws-env:feature1` 被删除了","attributes":{},"skip":false,"key":"1.3.2.15"},{"backlink":"./5.10.graph.html#fig1.6.10.1","level":"1.6.10","list_caption":"Figure: 生成的依赖图","alt":"生成的依赖图","nro":21,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2020-11-25/1606272727693-image.png","index":1,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"生成的依赖图","attributes":{},"skip":false,"key":"1.6.10.1"},{"backlink":"8.CloudPosse最佳实践.html#fig1.9.1","level":"1.9","list_caption":"Figure: Terraform 输出值的命名要对称","alt":"Terraform 输出值的命名要对称","nro":22,"url":"https://raw.githubusercontent.com/lonegunmanb/introduction-to-terraform-pic/master/2021-11-28/1638086002974-image.png","index":1,"caption_template":"图 _PAGE_LEVEL_/_PAGE_IMAGE_NUMBER_ - _CAPTION_","label":"Terraform 输出值的命名要对称","attributes":{},"skip":false,"key":"1.9.1"}]},"title":"《Terraform入门教程》","gitbook":"*"},"file":{"path":"6.test.md","mtime":"2024-05-25T00:11:31.216Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2024-06-24T12:39:39.308Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    

    </body>
</html>

